iOS 7 - UITextView size font to fit all text into view (no scroll)

对着背影说爱祢 提交于 2019-12-09 08:02:01

问题


I am trying to find a non-deprecated method to size the font of a textview down so that all text fits in the textview without requiring scrolling.

The method 'sizeWithFont' is deprecated and I want to ensure best practices, and XCode says to use 'boundingRectWithSize' but not sure how to use this to size a font down so that all text fits.

Any suggestions? And NO I can not use a UILabel instead. I need to have the text vertically aligned at the top and UILabel does not do this.

This worked Pre-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;
}

回答1:


Solution 1

Your problem can be solved by simply replacing sizeWithFont: constrainedToSize: with :

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

Solution 2

The sizeThatFits method can be used to address this problem like this:

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];
}

I hope one of these solutions solve your problem. Cheers!




回答2:


I had to do the same but then with programmatically added AutoLayout constraints (NSLayoutConstraint). Because of the constraints the contentSize wasn't correct for the most time causing the UITextView to scroll :/ To still get the correct font size I ended up just creating a new UITextView for the sake of doing some good ol' trial and error testing and getting it through there.

Pretty simple but like with everything you just gotta come up with it :)

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];



回答3:


Try UITextView's sizeThatFits. You can probably use it the following way:

//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;

}



回答4:


UITextView is subclass of UIScrollView -> so you can check the contentSize of the scrollable area after you set your text or font into textView.

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);

That should work... Probably it would work even without [textView layoutIfNeeded].




回答5:


Swift 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)


来源:https://stackoverflow.com/questions/23621536/ios-7-uitextview-size-font-to-fit-all-text-into-view-no-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!