我使用文本字段的所有屬性設置, 它不調整大小的字體,而編輯和文字走出鑑於文本框的UITextField不調整字體大小,而編輯
txtTitle.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
[txtTitle setMinimumFontSize:3.0];
[txtTitle setAdjustsFontSizeToFitWidth:YES];
它會調整在一定程度上那麼它的結果與文本超出視圖文本字段的結果相同
我使用文本字段的所有屬性設置, 它不調整大小的字體,而編輯和文字走出鑑於文本框的UITextField不調整字體大小,而編輯
txtTitle.font = [UIFont fontWithName:@"Helvetica" size:15.0f];
[txtTitle setMinimumFontSize:3.0];
[txtTitle setAdjustsFontSizeToFitWidth:YES];
它會調整在一定程度上那麼它的結果與文本超出視圖文本字段的結果相同
我知道它的一箇舊線程,但仍然無法識別。 這是一個錯誤,以下內容可以用作解決方法。 在UITextField上創建一個類別並編寫下列方法,並在需要更改字體大小以適應文本字段的寬度時調用它。
- (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];
}
謝謝Ankit!這個對我有用。我不得不更新過時的方法:sizeWithFont
- (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];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.text
attributes:@ { NSFontAttributeName: font }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){size.width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize labelSize = rect.size;
// 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];
}
斯威夫特答:
extension UITextField {
func adjustFontSizeToFitHeight(minFontSize : CGFloat, maxFontSize : CGFloat) {
guard let labelText: NSString = text else {
return
}
guard let font: UIFont = self.font else {
return
}
let labelHeight = frame.size.height
guard labelHeight != 0 else {
return
}
// replace with newton's method
for size in minFontSize.stride(to: maxFontSize, by: 0.1) {
let textHeight = labelText.sizeWithAttributes([NSFontAttributeName: font.fontWithSize(size)]).height
if textHeight > labelHeight {
self.font = font.fontWithSize(max(size - 0.1, minFontSize))
return
}
}
self.font = font.fontWithSize(maxFontSize)
}
}
儘管這段代碼可能有助於解決問題,但 提供了關於_why_和/或_how_的其他上下文,回答這個問題會顯着改善 長期價值。請[編輯]你的答案,添加一些 的解釋。 –
更新代碼,而不是使用sizeWithFont boundingRectWithSize。另外,考慮到佔位符。 [Gist here](https://gist.github.com/hermanccw/3fbf5df207854d0191b4) – Herman