18

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];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168

3 Answers3

59

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.

NSZombie
  • 1,857
  • 14
  • 14
  • 1
    `#define CGPointIntegral(point) CGPointMake((int)point.x,(int)point.y)` is a quick macro to cast CGPoint members to integers. – Andrew Feb 06 '12 at 02:17
  • What about rotated labels? They still appear blurry even using an integral frame (as expected, it becomes non-integral): http://stackoverflow.com/questions/10492367/how-do-i-achieve-a-clearly-rendered-rotated-uilabel – z8000 May 08 '12 at 04:05
  • 1
    Didn't work for me... Do all superviews need to be laid out with `CGRectIntegral`? – ilmiacs Feb 05 '13 at 08:48
  • Hi NSZombie Event adding CGRectIntegral I am getting Blurred text. – Sandeep Khade Apr 24 '13 at 11:48
  • Try to laid out the superview containing your label. If the superview itself is using float, your label might be blurred (although I never saw this kind of behaviour). – NSZombie Apr 24 '13 at 13:42
  • Worked for me, and of course, you can save yourself a line (if that's important to you) by using CGRectIntegral directly in the UILabel initialisation: `initWithFrame:CGRectIntegral(CGRectMake(yourXpos, yourYpos, yourWidth, yourHeight))` – Animal451 Feb 05 '14 at 12:20
  • +1 worked for me. Even if container views are floating values lables et blurred. – GoodSp33d Aug 08 '14 at 14:02
  • How would I do this if I'm using autolayout because won't autolayout ignore my manually set frame? – CoolPenguin Jul 14 '18 at 08:12
16

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
oskare
  • 1,061
  • 13
  • 24
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

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Legen Diary
  • 365
  • 3
  • 17