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
Here is my sample code. Basically, I check the expect size to know if font size need to increase or decrease. You need to add UITextViewDelegate to your class to make sure it workings
- (void)updateTextFont:(UITextView *)textView
{
// Only run if has text, otherwise it will make infinity loop
if (textView.text.length == 0 || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) return;
/*
- Update textView font size
If expectHeight > textViewHeight => descrease font size n point until it reach textViewHeight
If expectHeight < textViewHeight => inscrease font size n point until it reach textViewHeight
*/
CGSize textViewSize = textView.frame.size;
CGFloat fixedWidth = textViewSize.width;
CGSize expectSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
UIFont *expectFont = textView.font;
if (expectSize.height > textViewSize.height) {
while ([textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)].height > textViewSize.height) {
expectFont = [textView.font fontWithSize:(textView.font.pointSize-1)];
textView.font = expectFont;
}
} else {
while ([textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)].height < textViewSize.height) {
expectFont = textView.font;
textView.font = [textView.font fontWithSize:(textView.font.pointSize+1)];
}
textView.font = expectFont;
}
}