我正在使用所有属性设置的文本字段,而不是在编辑时调整字体大小,文本在文本字段中退出视图
txtTitle.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
[txtTitle setMinimumFontSize:3.0];
[txtTitle setAdjustsFontSizeToFitWidth:YES];
Run Code Online (Sandbox Code Playgroud)
它在某种程度上调整大小,然后与文本超出视图文本字段的结果相同
我知道这是一个旧线程,但仍然没有弄清楚.这是一个错误,以下内容可用作解决方法.在UITextField上创建一个类别并编写以下方法,并在需要更改字体大小以适合textfield的宽度时调用它.
- (void)adjustFontSizeToFit
{
UIFont *font = self.font;
CGSize size = self.frame.size;
for (CGFloat maxSize = self.font.pointSize; maxSize >= self.minimumFontSize; maxSize -= 1.f)
{
font = [font fontWithSize:maxSize];
CGSize constraintSize = CGSizeMake(size.width, MAXFLOAT);
CGSize labelSize = [self.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
// Keeping the width constant if given text requires more height , decrease the font size.
if(labelSize.height <= size.height)
{
self.font = font;
[self setNeedsLayout];
break;
}
}
// set the font to the minimum size anyway
self.font = font;
[self setNeedsLayout];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2538 次 |
| 最近记录: |