views:

1029

answers:

7
+3  A: 

OK I'm answering my own question here.

I found a number of references to this bug through Google, but everybody worked around it by playing with font sizes. After much hunting I found this thread that says anti-aliasing is applied when a view's frame contains fractional pixel values, e.g. if you calculate its size as a fraction of the super view.

Sure enough, casting the CGRect values to (int) for the view's frame worked perfectly. So as an example, if you wanted your text field to be centered vertically in the superview, you should use an (int) cast like this:

textFieldWidth = 300;
textFieldHeight = 31;
offsetX = 0;
offsetY = (superview.bounds.size.height - textFieldHeight) / 2;

textField.frame = CGRectMake((int) offsetX,
                             (int) offsetY,
                             (int) textFieldWidth,
                             (int) textFieldHeight);

There is also the CGRectIntegral function that you can use to convert a CGRect to integral values.

Mike Weller
+4  A: 

Use CGRectIntegral to make sure the text fields' frames are based on integer coordinates. You'll get fuzzy antialiasing when things lie on fractional coordinates.

Daniel Dickison
A: 

I would love to contribute as I just discovered the answer on my own after quite a bit of frustration.

The UITextField in InterfaceBuilder has a forced frame height of 31 pixels. This cannot be click-dragged to resize nor can it be set in the frame properties in IB. You need to go to viewDidLoad and adjust the frame height to 32 pixels, and this should solve the problem.

Hopefully future versions of IB will correct this.

Steve Lukas
+1  A: 

In addition to using non-fractional positioning one should make sure to use a non-centered vertical alignment for the UITextField. Looks like centered vertical alignment in combination with an odd font size results in blurred text, too.

Ortwin Gentz
A: 

i tried using CGRectIntegral and stuff. In my case changing min font size and font size in IB did it.

Fossli
A: 

I encountered this problem before. The solution below works perfectly for any text or frame size because it uses the round function to get rid of the fractional pixel values. Insert the following code after your instantiation of the uitextfield in question.

CGRect temp = textField.frame;
temp.origin.x = round(temp.origin.x);
temp.origin.y = round(temp.origin.y);
textField.frame = temp;
sd
A: 

Simply setting the UITextField frame height to an even value fixed it for me.

Sam V
This doesn't actually fix the problem though, it just works around the underlying issue so that you happen to get a size that doesn't cause the blurring. Forcing the frame into integral values fixes the problem properly.
Mike Weller
Well for me it did. I'm using systemFontOfSize:18
Sam V