OWolf
OWolf

Reputation: 5122

set the width of a UIText view based on the content size property

TextView.contentSize.width does not work to set the UITextView's .frame.size.width.

[TextView setFrame:CGRectMake(TextView.frame.origin.x, TextView.frame.origin.y, TextView.contentSize.width, TextView.contentSize.height)];  

Setting the UITextView's frame height to the contentSize.height property works to make the view's frame scale to the proper size for the current vertical size of the content. For some reason, the width of the view's frame does not respond in the same way. It just remains the same size regardless of the amount of text input.

When I log the contentsize of the UITextView dynamically, as I am typing in text to the view, the height property changes (at each line break), while the width does not. Makes me wonder what the width property is doing, what's it for.

Upvotes: 1

Views: 510

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

As depending upon width of UITextView it should be like this:

UIFont *myFont = [UIFont boldSystemFontOfSize:15.0]; //your font specification here
NSString *strText = yourTextView.text;
CGSize strsize = [strText sizeWithFont:myFont
                          forWidth:yourTextView.frame.size.width 
                    lineBreakMode:UILineBreakModeWordWrap]; //get string size
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x,yourTextView.frame.origin.y,strsize.width,strsize.height+10); //change accordingly

Upvotes: 1

Vikas S Singh
Vikas S Singh

Reputation: 1766

Try this way..

UIFont *font = [UIFont fontWithName:@"Enriqueta" size:15];
NSString *text = @"Your text";
CGSize frameSize = [text sizeWithFont:font];
CGRect originalFrame = textViewA1.frame;
textViewA1.frame = CGRectMake(CGRectGetMinX(originalFrame), CGRectGetMinY(originalFrame), frameSize.width, frameSize.height);

Upvotes: 1

Related Questions