Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找UITextView iOS 7中的行数_Ios_Objective C_Uitextview - Fatal编程技术网

查找UITextView iOS 7中的行数

查找UITextView iOS 7中的行数,ios,objective-c,uitextview,Ios,Objective C,Uitextview,我已经研究了几个小时,但在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. "

我已经研究了几个小时,但在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;
以及许多其他事情,但它只是说相同的错误的数字,每次,即使我改变字符串

[EDIT]答案经过编辑,提供了一个更好的解决方案/代码,其直接灵感来源于Apple文档

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

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)

来源和详细说明:

numLines的值与文本视图中的实际文本没有任何关系。它是给定高度和字体大小可以显示的行数。您真正想要什么值?我需要textView实际显示的行数。默认情况下,苹果拥有某种自动新品。我想知道这是如何决定何时换一条新线路的。你还不清楚。您想知道给定文本在文本视图中总共有多少行,还是想知道当前在文本视图中实际可见的给定文本行数?示例:很长的文本可能需要20行,即使文本视图只能显示5行,而与实际文本无关。或者,短文本此时可能只需要3行,即使文本视图可以显示5行。你想要这些例子中的3、5或20吗?我不需要知道文本能显示多少。我只想知道文本视图上实际显示了多少行,这样我就可以确定何时在我的应用程序中创建一个新的文本视图部分。Thank=我真的不懂这些示例..如果最后一个字符是“\n”,则会失败