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

前端 未结 5 1667
Happy的楠姐
Happy的楠姐 2021-02-12 22:58

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 \'sizeWithF

5条回答
  •  不要未来只要你来
    2021-02-12 23:14

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

提交回复
热议问题