![](/img/trans.png)
[英]Programmatically set UILabel has blurry text (integer frame values)
[英]Blurry UILabel when added programmatically
我正在為一個用於加載目的的視圖添加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
也會導致出現此問題。
對於那些無法使用之前答案的人,我發現如果我關閉autoresizingmask
功能,它會調用setShouldRasterize
方法。
[loadingText setTranslatesAutoresizingMaskIntoConstraints:NO];
因此,如果你評論這一行,它將正確顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.