以编程方式添加模糊UILabel

我正在为一个用于加载目的的视图添加UILabel。 但是,添加后它会变得模糊。 奇怪的是,我只是加载视图的相同代码,它加载在UITableViewController的顶部,它在那里工作得很好。 然而,这个在UIViewController之上是模糊的。

这是我的代码:

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]; 

您的标签模糊,因为框架使用浮动数字。

要为您的框架强制整数值,请执行以下操作:

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

您还可以将构成框架的所有值转换为int ,但CGRectIntegral会为您完成所有工作。

除了float / int问题之外,在UILabel的父视图上调用setShouldRasterize也会导致出现此问题。

对于那些无法使用之前答案的人,我发现如果我关闭autoresizingmaskfunction,它会调用setShouldRasterize方法。

 [loadingText setTranslatesAutoresizingMaskIntoConstraints:NO]; 

因此,如果你评论这一行,它将正确显示

Interesting Posts