Calculate the height of a UITextView's text with sizeWithFont:constrainedToSize:lineBreakMode doesn't seem to return correct results

旧城冷巷雨未停 提交于 2019-12-19 04:12:16

问题


I'm trying to calculate the 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


回答1:


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.



来源:https://stackoverflow.com/questions/3141399/calculate-the-height-of-a-uitextviews-text-with-sizewithfontconstrainedtosize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!