2

Say I have a UITextField that is sized at 90% of the width of it's superview and it's text alignment is centered. So it looks roughly looks like this:

[ |------Hello123------| ] frame size example: 0,0,310,60

     |frame?|             this is the frame I"m looking for

The view that is holding the textField contains many other objects like imageViews and other text fields. I have a method that unions all the frames of the subviews to find the smallest frame that everything fits inside of.

My issue is that each textField is always the same width regardless of the amount of content that was typed in. So the smallest frame is usually as wide as the label frame, even though the text does not fill the frame of the label. If there is only a few characters entered, then its frame should be very small for example.

I'd like a way to determine the rect where the text actually starts and stops, so I can use that as a frame value when trying to determine the smallest frame that will hold everything. Say the frame of the textField was 0,0,310,60. The area where the text inside is might be something like 30,0,50,60 for example.

I have tried textRectForBounds and editingRectForBounds methods on UITextField but they are returning basically the entire frame of the textfield. Probably I'm doing something wrong. Any help or snippets would be appreciated.

Thanks in advance for any help in determining the frame size around the text.

Adrian
  • 16,233
  • 18
  • 112
  • 180
CocoaEv
  • 2,984
  • 20
  • 21
  • http://stackoverflow.com/a/27724397/4475605 – Adrian Aug 05 '16 at 00:15
  • thanks @adrian that kinda/sorta works but I need the text field to hold the exact position and calling sizeThatFits() after every character causes it to move to a new location - so that is not good. Also, I have its frame set wider because it has auto shrinking font size - which doesn't work so well if the frame size keeps changing. Just grabbing the frame around the actual text minus any padding would be simplest. – CocoaEv Aug 05 '16 at 00:28

0 Answers0