Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITextField Auto Resize conform to the content

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?

like image 774
reizintolo Avatar asked Dec 31 '14 16:12

reizintolo


1 Answers

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: http://i.stack.imgur.com/udVlk.png

like image 86
Santiago Fabregat Avatar answered Sep 30 '22 19:09

Santiago Fabregat