How to Increase Width of Textfield According to Typed Text

How to increase width of textfield according to typed text?

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;
}

How to change TextField's height and width?

To adjust the width, you could wrap your TextField with a SizedBox widget, like so:

const SizedBox(
width: 100.0,
child: TextField(),
)

I'm not really sure what you're after when it comes to the height of the TextField but you could definitely have a look at the TextStyle widget, with which you can manipulate the fontSize and/or height

const SizedBox(
width: 100.0,
child: TextField(
style: TextStyle(fontSize: 40.0, height: 2.0, color: Colors.black),
),
)

Bear in mind that the height in the TextStyle is a multiplier of the font size, as per comments on the property itself:

The height of this text span, as a multiple of the font size.

When [height] is null or omitted, the line height will be determined
by the font's metrics directly, which may differ from the fontSize.
When [height] is non-null, the line height of the span of text will be a
multiple of [fontSize] and be exactly fontSize * height logical pixels
tall.

Last but not least, you might want to have a look at the decoration property of you TextField, which gives you a lot of possibilities

EDIT: How to change the inner padding/margin of the TextField

You could play around with the InputDecoration and the decoration property of the TextField. For instance, you could do something like this:

const TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 40.0),
),
)

How to increase width of textfield according to typed text?

Try with this:

<script>
function adjustWidth(obj) {

obj.style.width = (obj.value.length) * 7.3 + "px";
}
</script>
<input type="text" onkeypress="adjustWidth(this)">

However this depends on text size, font, etc

Adjust width of input field to its input

It sounds like your expectation is that the style be applied dynamically to the width of the textbox based on the contents of the textbox. If so you will need some js to run on textbox contents changing, something like this:

<input id="txt" type="text" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';">

Note: this solution only works when every character is exactly 8px wide. You could use the CSS-Unit "ch" (characters) which represents the width of the character "0" in the chosen font. You can read about it here.

Flutter TextField width should match width of contained text

Flutter has IntrinsicWidth to do the calculation for you. Just wrap your TextField or TextFormField in it as follow:

IntrinsicWidth(child: TextField())

Change UITextField width dynamically depends of content

To do this using Autolayout, you will need a constraint for the width of the text field. Then, when the text changes, calculate the desired width and set the constant property on the constraint to that value. Lastly, you will need to call layoutIfNeeded() on the parent view.

How can I change the width and height of a textfield in Vuetify?

You don't need to add CSS rules to change the v-text-field height, you could simply use the height prop like :

  <v-text-field height="350"></v-text-field>

but your component should be placed inside the template not in script tag :

 <template>
<v-text-field height="350"></v-text-field>
</template>

Resize textField Based On Content

You have to use an UITextView instead of an UITextField.

Then, you can use the sizeThatFits method.

But first you have to know how high one line will be. You can get that information by using lineHeight:

var amountOfLinesToBeShown: CGFloat = 6
var maxHeight: CGFloat = yourTextview.font.lineHeight * amountOfLinesToBeShown

After that, just call the sizeThatFits method inside your viewDidLoad method and set the maxHeight (line * 6) as your textview height:

yourTextview.sizeThatFits(CGSizeMake(yourTextview.frame.size.width, maxHeight))

How do I change text size and field size of a TextField?

You can change text size by changing the placeholder text's font size, which automatically adjusts the TextField's height.

In your case, this looks pretty similar to what you asked for:

HStack {
Group {
TextField($str, placeholder: Text("A"))
TextField($str, placeholder: Text("B"))
TextField($str, placeholder: Text("C"))
TextField($str, placeholder: Text("D"))
}
.frame(width: 60, height: nil)
.padding(.all, 5)
.textFieldStyle(.roundedBorder)
.font(Font.system(size: 60, design: .default))
.multilineTextAlignment(.center)
}


Related Topics



Leave a reply



Submit