Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase width of textfield according to typed text?

Tags:

I need to increase a text field's width according to its content. When the user inputs text, then the textfield size should increase automatically. I have one close (X) button next to this text field.

I have constrained the text field and button so that the text field is centered on screen, and the button is adjacent to it. (Text field should be editable, button should be clickable)

Text field size is this:

enter image description here

When I enter text in it, the size should automatically increase:

enter image description here

How can I achieve this?

like image 540
Mitesh jadav Avatar asked Jan 03 '17 05:01

Mitesh jadav


People also ask

How do you increase the width of a TextField?

To change the TextField width, you can simply wrap your TextField inside the SizedBox widget and give the appropriate width value. Here's how you do it: Step 1: Wrap your TextField inside the SizedBox widget. Step 2: Inside the SizedBox, Add the width parameter and assign the appropriate value.

How do I resize a text field?

Resize a text box Select the text box. Select one of the handles and drag until the text box is the size you want.

How do you increase the width of a textbox in HTML?

fontSize="14pt"; If you simply want to specify the height and font size, use CSS or style attributes, e.g. //in your CSS file or <style> tag #textboxid { height:200px; font-size:14pt; } <! --in your HTML--> <input id="textboxid" ...>

How do you expand a text field in flutter?

In Flutter, you can create an auto-resize as needed TextField (or TextFormField) in one of the following ways: Set the maxlines argument to null. The textfield can expand forever if a lot of text is entered. Set minLines to 1 and maxLines to N (a possitive integer).


1 Answers

I solve my problem : use this for textfield not go outside of screen.

 func getWidth(text: String) -> CGFloat {     let txtField = UITextField(frame: .zero)     txtField.text = text     txtField.sizeToFit()     return txtField.frame.size.width }  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {     let width = getWidth(textField.text!)     if UIScreen.mainScreen().bounds.width - 55 > width     {         txtWidthOfName.constant = 0.0         if width > txtWidthOfName.constant         {             txtWidthOfName.constant = width         }         self.view.layoutIfNeeded()     }     return true } 

Objective C Version

-(CGFloat)getWidth:(NSString *)text{     UITextField * textField = [[UITextField alloc]initWithFrame:CGRectZero];     textField.text = text;     [textField sizeToFit];     return textField.frame.size.width; }  -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {     if (self.textFieldName.isEditing == YES) {         CGFloat width = [self getWidth:textField.text];         if ([UIScreen mainScreen].bounds.size.width - 60 > width) {             self.txtWidthOfName.constant = 0.0;             if (width > self.txtWidthOfName.constant) {                 self.txtWidthOfName.constant = width;             }             [self.view layoutIfNeeded];         }     }     return YES; } 
like image 123
Mitesh jadav Avatar answered Sep 30 '22 21:09

Mitesh jadav