簡體   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