Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 'sizeWithFont' is deprecated and I want to ensure best practices, and XCode says to use 'boundingRectWithSize' but not sure how to use this to size a font down so that all text fits.

Any suggestions? And NO I can not use a UILabel instead. I need to have the text vertically aligned at the top and UILabel does not do this.

This worked Pre-iOS 7:

CGFloat fontSize;
CGFloat minSize;
if([deviceType isEqualToString:@"iPad"] || [deviceType isEqualToString:@"iPad Simulator"]){
    fontSize = 40;
    minSize = 15;
}
else{
    fontSize = 18;
    minSize = 8;
}
while (fontSize > minSize)
{
    CGSize size = [quote sizeWithFont:[UIFont fontWithName:@"Interstate" size:fontSize] constrainedToSize:CGSizeMake(newView.frame.size.width, 10000)];

    if (size.height <= newView.frame.size.height) break;

    fontSize -= 1.0;
}
like image 686
JimmyJammed Avatar asked May 13 '14 01:05

JimmyJammed


People also ask

How do I size a UITextView to its content?

It can be done using the UITextView contentSize . This will not work if auto layout is ON. With auto layout, the general approach is to use the sizeThatFits method and update the constant value on a height constraint. CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.

What is UITextView?

Overview. UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document. This class supports multiple text styles through use of the attributedText property ...

What is text view in swift?

A text view draws a string in your app's user interface using a body font that's appropriate for the current platform. You can choose a different standard font, like title or caption , using the font(_:) view modifier.


2 Answers

Solution 1

Your problem can be solved by simply replacing sizeWithFont: constrainedToSize: with :

boundingRectWithSize:CGSizeMake(newView.frame.size.width, FLT_MAX)
                options:NSStringDrawingUsesLineFragmentOrigin
             attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Interstate" size:fontSize]}
                context:nil];

Solution 2

The sizeThatFits method can be used to address this problem like this:

while (fontSize > minSize &&  [newView sizeThatFits:(CGSizeMake(newView.frame.size.width, FLT_MAX))].height >= newView.frame.size.height ) {
    fontSize -= 1.0;
    newView.font = [tv.font fontWithSize:fontSize];
}

I hope one of these solutions solve your problem. Cheers!

like image 141
aksh1t Avatar answered Dec 15 '22 01:12

aksh1t


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];
like image 26
bicycle Avatar answered Dec 15 '22 00:12

bicycle