9

How I can set the Auto resize in Text Field in iOS ?

helloWorld.m

self.TextFieldExample.text = @"HELLO WORLD HELLO WORLD HELLO WORLD HELLO WORLD";

Now:

HELLO WORLD HELLO ...

Correct:

HELLO WORLD HELLO WORLD HELLO WORLD HELLO WORLD

What is the best practice in this case?

Daniyar
  • 2,975
  • 2
  • 26
  • 39
reizintolo
  • 429
  • 2
  • 7
  • 20
  • 1
    Do you mean something like what was answered in this post? http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content – Chris Dec 31 '14 at 16:58
  • Unclear. Do you want the text field to use a smaller font to fit the text within its width or do you want the text field to be wider to fit its text? – rmaddy Dec 31 '14 at 17:05
  • user1390486 this post works, but not completely ... the text field resize is only in one line. – reizintolo Jan 01 '15 at 22:17
  • rmaddy, I want a multiline text field like in Android component – reizintolo Jan 01 '15 at 22:17
  • I think the best solution is with UITextField but I want resize the height automatically depending of the content, 2 lines, 5 lines, is there some way?. – reizintolo Jan 01 '15 at 22:29

4 Answers4

7

You should add an action executed by the textField of type Editing Changed and inside this action you should add [self.textField sizeToFit]; like this:

- (IBAction)textChanged:(UITextField *)sender {

    [self.textField sizeToFit];
}

This way every time that the contents of your Text Field changed it will execute the action and it will resize to the length of your text.

And if you want the text to resize to fit the width of your textField, you can do that in the attributes inspector of the storyboard: https://i.stack.imgur.com/udVlk.png

6

With a UITextField, text must fit on one line and cannot wrap.

You have two options:

  1. Shrink the font to fit on one line:

    self.TextFieldExample.adjustsFontSizeToFitWidth = YES; self.TextFieldExample.minimumFontSize = 10.0; //Optionally specify min size

  2. Use UITextView to enable text wrapping:

    See this answer: How to create a multiline UITextfield?

p.s. Per the style guide, "Properties should be camel-case with the leading word being lowercase." so you should rename your var to self.textFieldExample

Community
  • 1
  • 1
BFar
  • 2,447
  • 22
  • 23
  • BFar thanks for your response the point 2 is the way, but Do you know...How is possible increment the height according with the amount of the text automatically?? – reizintolo Jan 02 '15 at 15:43
  • after textfield lose focus and try to edit it again size will set to minimum size = 10 unfortunately this solution not work with me :( – Fadi Abuzant Aug 04 '17 at 12:58
0

In my case I wanted the font to be of the same size. I set textField width constraint to >= 10 and it just works fine on iOS 9, 10, 11.

For iOS 11 this even works while editing text.

To make this work while editing text on iOS 9 and 10, I handled one more method by registering for a textDidChange event.

//Swift 3 Sample

//In viewDidLoad
adjustableTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)


 @objc func textFieldDidChange(_ textField: UITextField) {
    adjustableTextField.resignFirstResponder()
    adjustableTextField.becomeFirstResponder()
}
Aditya Deshmane
  • 4,676
  • 2
  • 29
  • 35
-1

Swift:

self.TextFieldExample.adjustsFontSizeToFitWidth = true
self.TextFieldExample.minimumFontSize = 10.0
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53