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