Lucky
Lucky

Reputation: 609

How to get a height a UITextView should be given width and length of text?

I am adding a UIView that contains a UITextView which is constrained to the top, left, and bottom of the view.

The width of the UIView should be the screen size's width which in turn will be the UITextView's width.

When I go to create this UIView, I can make it's CGSize to the screen size's width, but I am not sure on how to calculate the height.

How can I figure out what height I must set this UIView so that the UITextView can properly show? Is there a way I can figure out the number of lines a UITextView would have given a certain width?

Upvotes: 0

Views: 1485

Answers (5)

Dallas Johnson
Dallas Johnson

Reputation: 1536

If you want to see all the text in the text view an have the surrounding views around it adapt to contain it the easiest way would be to use a UILabel instead. Then you could just use constraints with priorities. Set the UILabel number of lines to 0 so that it automatically wraps. Then set the contentCompressionResistancePriority to 1000 for vertical axis. Then you don't need to worry about the sizes. Let autolayout do the work for you.

Upvotes: 0

Marek R
Marek R

Reputation: 37512

First of all if you are using auto-layout this will be done out of the box, you don't have to do anything just setup constraints of auto-layout properly.

NSSString API is not very handy (as you can see in other answers) it is easy to miss something. I prfer use -(CGSize)sizeThatFits: from UIView.

UIView *textView = self.textView;
CGSize fitSize = { CGRectGetWidth(textView.frame), CGFLOAT_MAX };
CGSize fitSize = [textView sizeThatFits: fitSize];
NSLog(@"Your desired height: %f", fitSize.height);

It is more reliable and it should take into account all properties of text inside UITextView. I used this for UILabel didn't test for UITextView but it should work nicely.

Upvotes: 0

sandeep
sandeep

Reputation: 19

-(CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {

CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
                                      nil];

CGRect frame = [text boundingRectWithSize:constraintSize
                                  options:NSStringDrawingUsesLineFragmentOrigin
                               attributes:attributesDictionary
                                  context:nil];

CGSize stringSize = frame.size;

return stringSize;

}

Upvotes: 1

Rahul
Rahul

Reputation: 5834

You can use the following code to Get a dynamic Rect:

 NSString *myDynamicString = @"Hello World!";
 CGSize textRect = [myDynamicString boundingRectWithSize:CGSizeMake(MAX_WIDTH, MAX_HEIGHT)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName:[UIFont fontWithName:@"YouFontName" size:15]}
                                            context:nil];

Note

MAX_WIDTH Width allowed to expand the rect. If you give 200 then the rect will exapand till 200 and then break and Vice Versa for MAX_HEIGHT

Upvotes: 1

Aruna Mudnoor
Aruna Mudnoor

Reputation: 4825

Use this code

    CGSize constraintSize = CGSizeMake(textView.width - 2*textView.textContainer.lineFragmentPadding, CGFLOAT_MAX);
    CGFloat messageTextViewHeight = [text boundingRectWithSize:constraintSize
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                    attributes:@{NSFontAttributeName: textView.font}
                                                       context:nil].size.height;

Upvotes: 1

Related Questions