UITextField子类搞乱adjustFontSizeToWidth

Loa*_*dex 5 subclass objective-c uitextfield uikit ios

我在视图控制器中创建了一个子类文本视图,字体大小为15,启用了AdjustFontSizeToWidth,最小字体大小为10.我的textfield的占位符文本太大,无法容纳视图.将字体大小调整为宽度,系统将字体大小减小到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;
}
Run Code Online (Sandbox Code Playgroud)

占位符不适合屏幕

编辑:更新,我尝试使用此解决方案 并稍微调整代码,使其也适用于占位符.字体大小调整很好,但是当文本缩小时,UI会混乱,文本会在左图像视图后面向左偏移.在屏幕截图中,我们可以看到右边距太大而左边的文字太多. 截图失败新尝试

Cor*_*all 1

你不会错过任何东西——这是 iOS 的一个怪癖。minimumFontSize根据对类似问题的回答,小于 14 的值似乎被完全忽略。

将原始字体大小设置为 300,但字体UITextField仍为 14 pt,而不是 10 pt。

14 是一个奇数幻数;未记录,但可能用于保持可读性。如果能再低一点就好了。