繁体   English   中英

在UITextView iOS 7中查找行数

[英]Finding the number of lines in a UITextView iOS 7

我已经研究了几个小时,但在ios 7中没有找到一个可靠的答案。我有一个需求,我需要找出UITextView中显示了多少行文本。 我想知道该字符串在UITextView中占用了多少行

这是我要使用的字符串

self.txtView.text = @"Evolving from older bat-and-ball games, an early form of baseball         was being played in England by the mid-18th century. ";

我试过了

int numLines = self.txtView.contentSize.height/self.txtView.font.leading;

与许多其他事情一样,但是即使我更改了字符串,每次也只说相同(不正确)的数字。

[编辑]答案经过编辑以提供更好的解决方案/代码(直接从Apple文档获得启发)


您可以使用UITextViewNSLayoutManager来帮助您计算类似的内容

NSLayoutManager *layoutManager = [textView layoutManager];
unsigned nbLines, glyphIndex
unsigned numberOfGlyphs = [layoutManager numberOfGlyphs];
NSRange lineRange;

for (nbLines = 0, glyphIndex = 0; index < numberOfGlyphs; ++nbLines)
{
    [layoutManager lineFragmentRectForGlyphAtIndex:glyphIndex effectiveRange:&lineRange];
    glyphIndex = NSMaxRange(lineRange); // Skip to after last glyph of the line for next iteration
}
NSLog(@"Number of lines: %u", nbLines)

原始资料和详细说明: Apple的《文本布局编程指南》:“计数文本行”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM