2

I need to set up a TextField that automatically changes its width while typing. I though it was a simple task, but its turning out to be pretty challenging.

If I disable auto-layout, all I have to do to achieve this is to change the width of the TextField every time a new character is typed, using the following line:

TextField.frame.size.height = 30 + counter

(counter is related to the number of characters in the TextField).

Using auto-layout, this procedure doesn't work anymore. Does anybody have a solution to this problem? I haven't found a proper answer anywhere! Thanks!

Massi089
  • 41
  • 2
  • 4

2 Answers2

0

I think auto-layout added a constant "Width Equals" constraint to your text field. Try removing it and see if that works. Good luck!

nananta
  • 479
  • 4
  • 7
-3

You've to use UITextView for this and use it's sizeThatFits method. More info about it here

Community
  • 1
  • 1
Dhruv Ramani
  • 2,633
  • 2
  • 22
  • 29
  • Please select the answer,If I helped. – Dhruv Ramani Feb 14 '15 at 11:39
  • Thanks for your suggestion, but I am pretty sure there is a way to do the same thing using a UITextField instead of a UITextView – Massi089 Feb 14 '15 at 12:11
  • Actually you can have a multi-line UiTextField. Read the answer on this [question](http://stackoverflow.com/questions/1345561/how-to-create-a-multiline-uitextfield) – Dhruv Ramani Feb 14 '15 at 14:00
  • No, sorry, I didn't make myself clear. I I don't want a multi-line textfield (I have to write numbers, so it have to be on the same line), I want it to be single-line and to auto-adjust its width while typing. – Massi089 Feb 14 '15 at 15:18