Gabox Gabox - 4 months ago 56
iOS Question

Getting wrong height for UITextView using boundingRectWithSize:options:context

I'm getting the wrong height for an UITextView even I'm calculating its size like this:

CGRect requiredHeight = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)

When I add the height to my frame I can see all text but not the last line. I don't know why. (text was a
before I transformed it into a


so, the answer es very obvious but not always we see things like that so easily!.


The answer is if we need to get the height for an specific field (it could be label, textView, etc) we should use a method from those fields to calculate its height, due to they could have some specific properties, if we calculate the height from a text (not considering the field) we are not considering those special properties the field could have.


So short answer, for UITextView you should use [textView sizeThatFits:] to get its height, it's probably the best way to do it if you are not using auto layout.

advice: I encourage you to use autolayout if you can easily migrate your UI.