我正在使用 aUITextView
来显示一些文本。在布置文本时,我使用该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:
?