17

我正在尝试找到一种不推荐使用的方法来缩小 textview 的字体大小,以便所有文本都适合 textview 而无需滚动。

'sizeWithFont' 方法已被弃用,我想确保最佳实践,XCode 说要使用'boundingRectWithSize',但不确定如何使用它来缩小字体大小以使所有文本都适合。

有什么建议么?不,我不能使用 UILabel 代替。我需要让文本在顶部垂直对齐,而 UILabel 不这样做。

这在 iOS 7 之前有效:

CGFloat fontSize;
CGFloat minSize;
if([deviceType isEqualToString:@"iPad"] || [deviceType isEqualToString:@"iPad Simulator"]){
    fontSize = 40;
    minSize = 15;
}
else{
    fontSize = 18;
    minSize = 8;
}
while (fontSize > minSize)
{
    CGSize size = [quote sizeWithFont:[UIFont fontWithName:@"Interstate" size:fontSize] constrainedToSize:CGSizeMake(newView.frame.size.width, 10000)];

    if (size.height <= newView.frame.size.height) break;

    fontSize -= 1.0;
}
4

5 回答 5

21

解决方案 1

您的问题可以通过简单地替换来解决sizeWithFont: constrainedToSize:

boundingRectWithSize:CGSizeMake(newView.frame.size.width, FLT_MAX)
                options:NSStringDrawingUsesLineFragmentOrigin
             attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Interstate" size:fontSize]}
                context:nil];

解决方案 2

sizeThatFits方法可用于解决此问题,如下所示:

while (fontSize > minSize &&  [newView sizeThatFits:(CGSizeMake(newView.frame.size.width, FLT_MAX))].height >= newView.frame.size.height ) {
    fontSize -= 1.0;
    newView.font = [tv.font fontWithSize:fontSize];
}

我希望这些解决方案之一可以解决您的问题。干杯!

于 2014-05-21T13:01:44.483 回答
4

我必须做同样的事情,但随后以编程方式添加了 AutoLayout 约束 ( NSLayoutConstraint)。由于限制,contentSize大多数时间都不正确导致UITextView滚动:/为了仍然获得正确的字体大小,我最终只是创建了一个新UITextView的,以便进行一些好的试错测试并通过它那里。

非常简单,但就像你必须想出的所有东西一样:)

NSInteger fontSize = 200;

UITextView *testTextView = [[UITextView alloc] init];
testTextView.text = self.myRealTextView.text;
testTextView.font = [UIFont fontWithName:@"AldotheApache" size:fontSize];
while ([testTextView sizeThatFits:(CGSizeMake(self.myRealTextView.frame.size.width, FLT_MAX))].height >= self.myRealTextView.frame.size.height ) {
    fontSize -= 0.5;
    testTextView.font = [UIFont fontWithName:@"AldotheApache" size:fontSize];
}

NSLog(@"Correct font size is: %ld",(long)fontSize);

self.myRealTextView.font = [UIFont fontWithName:@"AldotheApache" size:fontSize];
于 2016-03-21T21:55:07.867 回答
1

UITextView 是 UIScrollView -> 的子类,因此您可以在将文本或字体设置为 textView 后检查可滚动区域的 contentSize。

textView.text = quote;
do
{
    textView.font = [UIFont fontWithName:@"Interstate" size:fontSize];
    [textView layoutIfNeeded];
    if (textView.contentSize.height <= newView.frame.size.height) {
        break;
    }
    fontSize -= 1.0;
} while (fontSize >= minSize);

这应该可以工作......即使没有[textView layoutIfNeeded],它也可能会工作。

于 2014-05-21T20:01:49.100 回答
1

试试 UITextView 的sizeThatFits. 您可能可以通过以下方式使用它:

//Set  text content of UITextView
//...

while (fontSize > minSize) {

   // Set font size of UITextView

   CGSize size = [textView sizeThatFits:(CGSizeMake(textView.frame.size.width, FLT_MAX)];

   if (size.height <= newView.frame.size.height) break;

   fontSize -= 1.0;

}
于 2014-05-15T15:05:00.097 回答
0

斯威夫特 5+

    var fontSize = Font.Size.section.rawValue
    let minSize = Font.Size.large.rawValue

    while fontSize > minSize {
        let textViewSize = CGSize(width: textView.frame.size.width, height: textView.frame.size.height)
        let size = textView.sizeThatFits(textViewSize)

        if size.height <= textViewSize.height {
            break
        }

        fontSize -= 1.0
    }

    textView.font = Font.primary.of(size: fontSize)
于 2019-11-28T22:15:37.750 回答