IOS UITextField Auto Resize matches content

How can I set auto-resizing in a text box in iOS?

helloWorld.m

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

Now:

HEALTHY WORLD HELLO ...

Right:

WORLD WORLD OWNERS WORLD WORLD OWNERS

What is the best practice in this case?

+6
source share
4 answers

With a UITextField, the text should fit on one line and cannot be completed.

You have two options:

  • Set the font on one line:

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

  • Use UITextView to enable text wrapping:

    See this answer: How to create a multi-line UIText field?

ps In the style guide , "Properties must be a camel case and the leading word must be lowercase." so you have to rename your var to self.textFieldExample

+2
source

You must add the action performed by textField type Editing Changed , and inside this action you must add [self.textField sizeToFit]; in the following way:

 - (IBAction)textChanged:(UITextField *)sender { [self.textField sizeToFit]; } 

This method, every time the contents of your text field is resized, takes an action and resizes it to the length of your text.

And if you want the text to fit the width of your text field, you can do this in the storyboard attribute inspector: http://i.stack.imgur.com/udVlk.png

+7
source

Swift:

 self.TextFieldExample.adjustsFontSizeToFitWidth = true self.TextFieldExample.minimumFontSize = 10.0 
0
source

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

For iOS 11, this even works when editing text.

To do this, editing text in iOS 9 and 10, I processed another method by logging 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() } 
0
source

Source: https://habr.com/ru/post/980376/


All Articles