草庐IT

ios - enumerateLineFragmentsForGlyphRange :withBlock: returns word fragments

coder 2024-01-24 原文

我正在使用 UITextView 来显示一些文本。在布置文本时,我使用 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: 方法中的无效换行符?

最佳答案

虽然我仍然不确定是什么导致了上述潜在问题,但我至少找到了解决问题的方法:https://stackoverflow.com/a/19603172/686902

关于ios - enumerateLineFragmentsForGlyphRange :withBlock: returns word fragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028484/

有关ios - enumerateLineFragmentsForGlyphRange :withBlock: returns word fragments的更多相关文章

随机推荐