Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to reliably calculate the height of a UITextView?

I've tried sizeWithFont:constrainedToSize:lineBreakMode and sizeToFit, both of which don't return the correct result.

The property contentSize is supposed to return the correct height, but it doesn't work until the text view is rendered to the screen, and I need to calculate the height before the text view is visible (it determines the height of a UITableViewCell.

Has anyone found any other method, a custom text view or the like, that correctly calculates the height of a UITextView?

EDIT I should clarify that I want the ideal height based upon the text view's content.

like image 420
Quentamia Avatar asked Nov 26 '22 10:11

Quentamia


1 Answers

IOS 7 does not supports the property contentSize anymore. Try using this

CGFloat textViewContentHeight = textView.contentSize.height;
textViewContentHeight = ceilf([textView sizeThatFits:textView.frame.size].height + 9);

this fixed my problem.

like image 124
gunas Avatar answered Dec 01 '22 01:12

gunas