简体   繁体   English

iOS Swift UITextfield 调整字体大小宽度

[英]iOS Swift UITextfield adjust fontsize width

How to adjust UITextfield font size when it is hold long character?保持长字符时如何调整 UITextfield 字体大小? I tried adjustsFontSizeToFitWidth.我试过adjustsFontSizeToFitWidth。 it is not working.它不工作。 any help will be appricated.thanks in advance任何帮助将被appricated.Thanks提前

With a UITextField, text must fit on one line and cannot wrap.使用 UITextField,文本必须适合一行并且不能换行。

You have two options:您有两个选择:

Shrink the font to fit on one line:缩小字体以适合一行:

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

Use UITextView to enable text wrapping:使用 UITextView 启用文本换行:

Working Swift 4 Solution工作 Swift 4 解决方案

Make sure you set adjustsFontSizeToFitWidth to true and then set your minimumFontSize .确保将adjustsFontSizeToFitWidth设置为true ,然后设置minimumFontSize This will scale the text inside the UITextField to fit the width of your UITextField with a minimum font size equal to or greater than what you set minimumFontSize to.这将缩放中的文本UITextField ,以满足您的宽度UITextField用最小字体大小等于或小于你设定更大的minimumFontSize来。 When the text is still too long to scale down to fit at the minimum font size it will truncate the rest.当文本仍然太长而无法缩小以适应最小字体大小时,它将截断其余部分。

textField.adjustsFontSizeToFitWidth = true
textField.minimumFontSize = 10

or you can go to the Main.storyboard file and click on the text field you want to work with and then go to the identity inspector and select the minimum font size that you want to have :) enjoy coding If you have issues with this method check the photo down below: 或者,您可以转到Main.storyboard文件,然后单击要使用的文本字段,然后转到身份检查器,然后选择想要的最小字体大小:)享受编码如果此方法有问题查看下面的照片:

enter image description here 在此处输入图片说明

You have to set the width first你必须先设置宽度

textField.frame = CGRect(0, 0, your width, your height)

And并且

textField.minimumFontSize = 0.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM