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

前端 未结 5 1593
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:02

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

提交回复
热议问题