2014-09-25 235 views
2

我正在使用UITextView來顯示一些文本。在佈置文本時,我使用enumerateLineFragmentsForGlyphRange:withBlock:方法列舉了文本行。enumerateLineFragmentsForGlyphRange:withBlock:返回單詞片段

NSInteger shrunkNumberOfLines = 3; 
__block NSMutableString *shortenedText = [NSMutableString new]; 
__block NSInteger currentLine = 0; 
__block BOOL needsTruncation = NO; 

[detailsTableViewCell.descriptionTextView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, text.length) usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) { 
    if (currentLine < shrunkNumberOfLines) { 
    NSRange stringRange = ((glyphRange.length + glyphRange.location) <= text.length) ? glyphRange : NSMakeRange(glyphRange.location, (text.length - glyphRange.location)); 
      NSString *appendString = [text substringWithRange:stringRange]; 
      NSLog(@"%@", appendString); 
      [shortenedText appendString:appendString]; 
      currentLine += 1; 
    } else { 
    needsTruncation = YES; 
    *stop = YES; 
    } 
}]; 

不過,我遇到一個奇怪的錯誤:通常情況下,這被顯示在TextView的文本不與我在appendString看到的文字排隊。

例如,在文本字段中的文本可能會說:

President Obama offered a 
blueprint for deeper American 
engagement in the Middle East. 

...但看着我的NSLog語句,這些appendStrings是這樣的:

President Obama offered a blu 
eprint for deeper American en 
gagement in the Middle East. 

我已經嘗試了一堆東西 - 玩hyphenationFactor,確保textContainerInsets是正確的,等等 - 但我不明白這一點。 enumerateLineFragmentsForGlyphRange:withBlock:方法中導致無效換行符的原因是什麼?

回答