16

When my UITextField becomes first responder(on edit) the text is moving down very slightly and the spacing between the characters is increased very slightly. Upon resign first responder(keyboard goes away), the characters move back to the original positioning. At first I thought this was a font issue, as I am using a custom font, but the same thing happens when I use the system font or other custom fonts.

Please check the images below. The first one is with the keyboard down and the second is with the keyboard up. It may be difficult to see, as the variance is small, but the keyboard up state shows the characters moved down and apart slightly.

enter image description here

enter image description here

jasper holgren
  • 233
  • 2
  • 7
  • 2
    Can you show more code? What properties are set for it? How is it added to the view? – AdamG Aug 31 '13 at 00:08
  • I am adding it to the view via a nib file, and I am not doing anything with it programatically. I have a custom font, center alignment, no border, no clear button, adjust to fit and everything else is just default settings. – jasper holgren Aug 31 '13 at 01:16
  • I noticed this is happening since iOS 10.0 (but not 9.3.x) when using invisible border style. Default border style does not have the issue. This answer helped me: http://stackoverflow.com/a/41823255/129202 – Jonny Feb 06 '17 at 07:40

9 Answers9

4

In xib, set textfield's borderStyle to any value than TextBorderStyleNone . And in code, set textfield's borderStyle as TextBorderStyleNone explicitly.

I could'n figure out why but it seems like initializer bug. It works fine in other code which doesn't initialize UITextField by outlet from xib.

When set textfield's borderStyle as TextBorderStyleNone at xib, the problem still remains even if set borderStyle as TextBorderStyleNone in code. It may be followed by some kind of 'layoutIfNeeded' stuff, which triggered by value change.

Box Jeon
  • 91
  • 4
3

Selecting the "adjust to fit" property sets the property adjustsFontSizeToWidth in UITextField.

That has this property (UITextField Apple Documentation):

Normally, the text field’s content is drawn with the font you specify in the font property. If this property is set to YES, however, and the contents in the text property exceed the text field’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. The text is shrunk along the baseline.

The default value for this property is NO. If you change it to YES, you should also set an appropriate minimum font size by modifying the minimumFontSize property.

I would try first unchecking this and seeing if that would stop it, and then follow the documentation advice and set the minimumFontSize property lower otherwise, which may also be causing the problem.

This issue could possibly be a result of the appearance of the blue "now editing" cursor. The baseline of a text (reference) is the bottom of the text. Your text is shrinking "along the baseline" by pushing down, which leans more strongly in the direction of it being a minimumFontSize/adjustsFontSizeToWidth issue.

AdamG
  • 3,718
  • 2
  • 18
  • 28
  • Thank you for the response. Neither setting the adjustsFontSizeToWidth property to NO(unchecking it) or modifying the minimum font size fixed the issue. – jasper holgren Aug 31 '13 at 03:09
  • When you set the minimumfontsize did you make it the same size as your font? – AdamG Aug 31 '13 at 03:13
  • I just tried that to no avail. I found this post which helps with the horizontal positioning, but the text spacing still changes. http://stackoverflow.com/questions/6351340/using-a-custom-font-in-a-uitextfield-causes-it-to-shift-slightly-when-accessed – jasper holgren Aug 31 '13 at 03:28
  • From that post, did you try changing the editing inset value to the left (the 8 of the 8, 5 in the bottom example?) – AdamG Aug 31 '13 at 04:17
  • Ya, changing the first value just shrunk the UITextField rect. – jasper holgren Aug 31 '13 at 04:29
3

If you are forcefully making UITextField first responder, do it in viewDidAppear: instead of viewDidLoad... This solved the problem for me...

Shahrukh Malik
  • 242
  • 2
  • 15
2

Checking OFF the "Clip to Bounds" of textField in my storyboard works fine for me.

e3c8
  • 119
  • 1
  • 3
  • This helped me. As noted in the comment to OT above, I noticed this is happening since iOS 10.0 (but not 9.3.x) when using invisible border style. Default border style does not have the issue. – Jonny Feb 06 '17 at 07:43
1

You have probably sized your UITextField to be slightly too small height-wise. Try manually resizing it so it's a pixel or two taller. Or select it in the XIB editor and use "Size to fit content" in the Editor menu. That ought to set the text field to the right size so that this text shifting doesn't happen.

1

Setting the Min Font Size property equal to the font size in Interface Builder worked for me

shoe
  • 952
  • 1
  • 20
  • 44
1

Changing the vertical alignment under control in the storyboard did it for me.

Andy
  • 866
  • 9
  • 14
1

My issue was connected with wrong alignment of UIStackView.

When you put UITextField into UIStackView, make sure you set its alignment correctly, otherwise auto-layout won't figure out how to position your view right.


In my case, I had to set it to .leading.

stackView.alignment = .leading
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
0

You should set Font property earlier than Text property.

Alex
  • 172
  • 1
  • 6