Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is UILabel's text blurry on iPad if width is not even?

Following phenomenon: my text is "Search". I create a UILabel of SmallSystemFontSize and call sizeToFit:.

The result is 39 units wide and the text looks kind of blurry. If I adjust the width to 40 it looks perfect.

I read that the text gets blurry if you hit sub pixels, meaning the width would be something like 39.5, but it seems it has to be even.

Can somebody confirm or even explain what is going on ?

like image 557
Krumelur Avatar asked Nov 10 '11 14:11

Krumelur


People also ask

What is UILabel?

A view that displays one or more lines of informational text.

How do you wrap text in UILabel?

If you set numberOfLines to 0 (and the label to word wrap), the label will automatically wrap and use as many of lines as needed. If you're editing a UILabel in IB, you can enter multiple lines of text by pressing option + return to get a line break - return alone will finish editing.


2 Answers

In my case, having set shouldRasterize = YES on the CGLayer of the UILabel's superview was the culprit. Removing that line made the text nice and crisp.

like image 158
Johannes Fahrenkrug Avatar answered Sep 18 '22 23:09

Johannes Fahrenkrug


UIView items are positioned by their center which for a size that is odd is on a half pixel, 19.5 for a width of 39.. This alignment causes pixel averaging that causes the fuzziness.

One way is to make it an even width.

Another is to place it by the center at an even point use:

@property(nonatomic) CGPoint center

Example, for a desired position of label; at (10, 10, 39, 19) one could use:

label.center = CGPointMake(50, 20);
like image 24
zaph Avatar answered Sep 18 '22 23:09

zaph