Jen Yuaree
Jen Yuaree

Reputation: 233

UITextfield text is moving on edit

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

Upvotes: 16

Views: 5572

Answers (9)

Robert Dresler
Robert Dresler

Reputation: 11140

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

Upvotes: 1

Andy
Andy

Reputation: 876

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

Upvotes: 1

shoe
shoe

Reputation: 1070

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

Upvotes: 1

e3c8
e3c8

Reputation: 119

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

Upvotes: 2

Box Jeon
Box Jeon

Reputation: 91

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.

Upvotes: 4

Shahrukh Malik
Shahrukh Malik

Reputation: 252

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

Upvotes: 3

Alex
Alex

Reputation: 322

You should set Font property earlier than Text property.

Upvotes: 0

AdamG
AdamG

Reputation: 3718

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.

Upvotes: 3

buzzardsoft
buzzardsoft

Reputation: 56

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.

Upvotes: 1

Related Questions