Blurry UILabel when added programmatically

╄→尐↘猪︶ㄣ 提交于 2019-12-17 23:07:38

问题


I am adding a UILabel to a view meant for loading purposes. However, it gets blurry after I added it. The weird thing is that I just about the same code for an loading view which is loaded ontop of an UITableViewController and it works great there. Yet, this one on top of an UIViewController is blurry.

This is my code:

float x = (self.view.frame.size.width-20)/2;
float y = (self.view.frame.size.height-70)/2;

// Add loading and sub text
UILabel *loadingText = [[UILabel alloc] initWithFrame:CGRectMake(10, round(y+30), round(self.view.frame.size.width-20), 21)];
[loadingText setTextAlignment:UITextAlignmentCenter];
[loadingText setNumberOfLines:0];
[loadingText setFont:[UIFont systemFontOfSize:17]];
[loadingText setText:NSLocalizedString(@"Please wait...\nWe are processing your request", @"LoadingPage")];
[loadingText sizeToFit];
[loadingText setBackgroundColor:[UIColor clearColor]];
[loadingText setCenter:self.view.center];
[loadingText setTag:2];

[view addSubview:loadingText];

回答1:


Your label is blurry because the frame is using floating numbers.

To force integers value for your frame just do :

[loadingText setFrame:CGRectIntegral(loadingText.frame)];

You could also cast all your values composing your frame to int, but CGRectIntegral does all the job for you.




回答2:


Apart from the float/int issue, calling setShouldRasterize on the parent view of the UILabel can also cause this problem to appear.




回答3:


For those of you that couldn't use the previous answers, I found that if I turned off the autoresizingmask feature it called the setShouldRasterize method.

[loadingText setTranslatesAutoresizingMaskIntoConstraints:NO];

thus if you comment this line out, it will display correctly



来源:https://stackoverflow.com/questions/8065892/blurry-uilabel-when-added-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!