Resize font size to fill UITextView?

后端 未结 15 987
春和景丽
春和景丽 2020-12-01 07:03

How would I set the font size of text in a UITextView such that it fills the entire UITextView? I\'d like the user to type in their text, then have the text fill the entire

15条回答
  •  醉酒成梦
    2020-12-01 07:31

    An updated version of @Arie Litovsky's code. This works in iOS7 and later and stretches the font size both up and down so the text fits.

    - (void) stretchFontToFit:(UITextView*)textView
    {
    
        while( textView.contentSize.height < textView.frame.size.height )
        {
            textView.font = [textView.font fontWithSize:textView.font.pointSize +1];
            [textView layoutIfNeeded];
        }
    
        while( textView.contentSize.height > textView.frame.size.height )
        {
            textView.font = [textView.font fontWithSize:textView.font.pointSize -1];
            [textView layoutIfNeeded];
        }
    }
    

提交回复
热议问题