使用sizeWithFont计算UITextView的文本的高度:constrainedToSize:lineBreakMode似乎不返回正确的结果 [英] Calculate the height of a UITextView's text with sizeWithFont:constrainedToSize:lineBreakMode doesn't seem to return correct results

查看:118
本文介绍了使用sizeWithFont计算UITextView的文本的高度:constrainedToSize:lineBreakMode似乎不返回正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算由UITextView约束的文本的高度,但它似乎不返回正确的结果。



这是我的代码: / p>

   - (void)textViewDidChange:(UITextView *)aTextView {
CGSize textSize = [aTextView.text sizeWithFont:aTextView。 font constrainedToSize:aTextView.frame.size lineBreakMode:UILineBreakModeWordWrap];
counter.text = [NSString stringWithFormat:@%f,textSize.height];
}

您可以下载示例项目和一个简短的屏幕录像,说明问题(418 KB)



总而言之,问题是当我在一行的末尾键入一个长字时,该字被移动到下一行,但是当字符串的高度未被正确调整时



任何帮助感谢。



最好,

托马斯。 >

PS :它发生在iPhone SDK 3.1.3

解决方案

问题是UITextField在它实际上绘制和包装文本。据我所知,没有程序化的方式来获得这个尺寸,但只是从尺寸减去6左右应该使它的行为适当。


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

解决方案

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.

这篇关于使用sizeWithFont计算UITextView的文本的高度:constrainedToSize:lineBreakMode似乎不返回正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆