【发布时间】:2015-06-11 15:58:04
【问题描述】:
我在视图控制器中创建了一个子类文本字段视图,字体大小为 15,启用了 AdjustFontSizeToWidth,最小字体大小为 10。 我的文本字段有一个占位符文本太大而无法容纳视图。已激活将字体大小调整为宽度,系统正在将字体大小减小到 14。我不知道为什么14,因为占位符仍然不适合(见截图) 知道为什么会这样吗?我错过了什么吗? 我继承了 UITextfield 并覆盖了以下方法(不确定它是否相关,但这似乎是可能导致此错误的方法):
- (CGRect)leftViewRectForBounds:(CGRect)bounds {
CGRect leftViewRect = [super leftViewRectForBounds:bounds];
if ([self shouldShowLeftView] == YES) {
CGFloat newHeight = self.frame.size.height * 0.5f;
if (self.leftImage) {
leftViewRect.size.width = newHeight;
}
leftViewRect.origin.x = kMargins;
leftViewRect.origin.y = (self.frame.size.height - leftViewRect.size.height) / 2.0f;
}
return leftViewRect;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rectForBounds = [super textRectForBounds:bounds];
if ([self shouldShowLeftView] == YES) {
rectForBounds.origin.x += kMargins;
rectForBounds.size.width -= 2.0 * kMargins;
} else {
rectForBounds.origin.x += kMargins / 2.0;
rectForBounds.size.width -= 2.0 * (kMargins / 2.0);
}
return rectForBounds;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect rectForBounds = [super editingRectForBounds:bounds];
if ([self shouldShowLeftView] == YES) {
rectForBounds.origin.x += kMargins;
rectForBounds.size.width -= 2.0 * kMargins;
} else {
rectForBounds.origin.x += kMargins / 2.0;
rectForBounds.size.width -= 2.0 * (kMargins / 2.0);
}
return rectForBounds;
}
编辑:更新,我尝试使用this solution 并稍微修改了代码以使其也适用于占位符。字体大小调整很好,但是当文本缩小时 UI 会变得混乱,文本本身向左偏移,位于左侧图像视图的后面。在截图中,我们可以看到右边距太大,左边的文字太多。
【问题讨论】:
-
存在系统最小字体大小。 UITextField 不会像您希望的那样缩小字体大小。
-
我的最小字体大小是10,占位符有一个14pt的字体,而不是我默认给的15,可能是计算错误。这就是我问的原因。
标签: ios objective-c uitextfield uikit subclass