UITextField in iOS 8.1 have different vertical text alignment between editing and not editing mode when using some fonts

Originator:diogot
Number:rdar://19374610 Date Originated:2015/01/05
Status:Open Resolved:
Product:iOS Product Version:8.1.2
Classification:UI Reproducible:Always
 
Summary:
UITextField in iOS 8.1 have different vertical text alignment between editing and not editing mode when using some fonts like Helvetica Neue Light 17.

Steps to Reproduce:
- Add a UITextField
- Set the font to Helvetica Neue Light 17 or a text style like UIFontTextStyleBody or UIFontTextStyleHeadline. May happen with other fonts
- Run
- Tap on the text field

Expected Results:
The text don't moves

Actual Results:
When the text field becomes the first responder the text moves up a few pixels

Version:
iOS 8.1.2 (12B440)

Notes:
Example project https://github.com/diogot/UITextFieldBug

Configuration:
All tested devices (and simulator) with iOS 8.1, iOS 8.0 not tested. In iOS 7.1 the bug is not present.

Comments

Workaround

The following snippet in a UITextField subclass should fix the issue:

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    if (UIDevice.currentDevice.systemVersion.integerValue != 8) return [self textRectForBounds:bounds];

    CGFloat const scale = UIScreen.mainScreen.scale;
    CGFloat const preferred = self.attributedText.size.height;
    CGFloat const delta = ceil(preferred) - preferred;
    CGFloat const adjustment = floor(delta * scale) / scale;

    CGRect const textRect = [self textRectForBounds:bounds];
    CGRect const editingRect = CGRectOffset(textRect, 0.0, adjustment);

    return editingRect;
}

See http://stackoverflow.com/a/30676237/796103 for a detailed explanation.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!