我如何知道文本是否超出文本视图的范围? [英] How do I know if text exceeds the bounds of a text view?
问题描述
我想知道文本何时超出UITextView的界限并要求用户滚动。知道某个字体占用多少行是没用的 - 我怎么知道一定数量的文本会占用多少行?
I would like to know when text exceeds the bounds of a UITextView and requires the user to scroll. Knowing how many lines a certain font will occupy is useless - how do I know how many lines a certain amount of text will take up?
在上图中,我会添加淡入淡出效果,我可以优雅地切断文本视图。但是,如果没有要滚动的文本,则显示淡入淡出效果看起来不正确。
In the image above, I would add a fade effect so that I can cut off the text view gracefully. However, it does not look correct to display the fade if there is not enought text to scroll.
推荐答案
获取<$ c $设置文本后c> contentSize 。这是一个CGSize, height
将是UITextView在不滚动的情况下显示文本所需的高度。
Get contentSize
after setting the text. It's a CGSize and the height
will be the height required for the UITextView to display the text without scrolling.
myTextView.text = someText;
CGSize textSize = myTextView.contentSize;
float verticalSpaceNeededByText = textSize.height;
这篇关于我如何知道文本是否超出文本视图的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!