Calculate the height of a UITextView's text with sizeWithFont:constrainedToSize:lineBreakMode doesn't seem to return correct results
I'm trying to calculate t开发者_高级运维he height of the text constrained by a UITextView but it doesn't seem to return correct results.
Here is my code :
- (void)textViewDidChange:(UITextView *)aTextView {
CGSize textSize = [aTextView.text sizeWithFont:aTextView.font constrainedToSize:aTextView.frame.size lineBreakMode:UILineBreakModeWordWrap];
counter.text = [NSString stringWithFormat:@"%f", textSize.height];
}
You can download the sample project and a short screencast that illustrates the problem (418 KB).
In summary, the problem is that when I type a long word at the end of a line, the word is moved to the next line, but the height of the string isn't correctly adjusted when it happens.
Any help appreciated.
Best,
Thomas.P.S. : It happens with the iPhone SDK 3.1.3
The issue is that the UITextField has a slight margin around the area it actually draws and wraps the text. As far as I know there is no programatic way to get this size, but just subtracting 6 or so from the size should make it behave appropriately.
精彩评论