3

I've got a strange (and very annoying) bug while trying to horizontally centering some text within a UITextField.

I have placed a UITextField using Storyboard with plain-text (not attributed) setting the horizontal alignment to center. Text appears correct aligned (centered) in storyboard but gives different results for different versions of iOS and between simulator and real device:

Simulator iPad with iOS 7.0:

  • non-editing mode: centered
  • editing mode: centered
  • after leaving editing mode: centered

Simulator iPad with iOS 7.1:

  • non-editing mode: left aligned
  • editing mode: centered
  • after editing mode: centered

iPad Air with iOS 7.1:

  • non-editing mode: left aligned
  • editing mode: centered
  • after editing mode: left aligned

Setting the NSTextAlignmentCenter before or after setText has no effect.

Is this a bug in iOS 7.1? Does anyone got the same problem or even a solution for that?

Heiko
  • 191
  • 1
  • 5

2 Answers2

1

I had the similar problem in iOS8. Look like an iOS bug to do with making the text selectable.

If you make the text selectable in the Storyboard, it all works OK. If its NOT selectable in the Storyboard, it always left justifies.

My UITextView is in a table cell, and I found I can setSelectable:NO in cellForRowAtIndexPath and it still aligns correctly (centred in my case) with selectable on in the Storyboard.

Tested in simulator (iOS 7.1 and 8.1) and on device (iOS 8.1).

Rob
  • 1,037
  • 1
  • 13
  • 20
0

I had the same bug and this is my fix. For the UITextView, set its text alignment in code and call its method layoutIfNeeded(). I did that after setting the text, if that makes a difference to anyone.

I tested it on simulator for iOS 8.4 and Xcode 6.4. Hope it helps!