从NSAttributedString中提取最后50行

问题描述 投票:5回答:2

是否有一个简单的方法来分割NSAttributedString所以我只得到最后50个左右的线?

NSMutableAttributedString *resultString = [receiveView.attributedText mutableCopy];
[resultString appendAttributedString:[ansiEscapeHelper attributedStringWithANSIEscapedString:message]];
if ([[resultString.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count]>50) {
    //resultString = [resultString getLastFiftyLines];
}
ios split nsmutableattributedstring
2个回答
3
投票

是否有一种简单的方法来分割NSAttributedString所以我只得到最后50行?

不需要。您将需要请求string并确定您感兴趣的范围,然后使用NSAttributedString等API创建源自源的新- [NSAttributedString attributedSubstringFromRange:]表示:

- (NSAttributedString *)lastFiftyLinesOfAttributedString:(NSAttributedString *)pInput
{
  NSString * string = pInput.string;
  NSRange rangeOfInterest = ...determine the last 50 lines in "string"...;
 return [pInput attributedSubstringFromRange:rangeOfInterest];
}

2
投票

您可以使用AttributedString的substring方法:

if ([resultString length]>50) {
  resultString = [resultString attributedSubstringFromRange:NSMakeRange(0, 50)];
}

NSMakeRange - 0告诉我们从哪里开始,50是子串的长度

© www.soinside.com 2019 - 2024. All rights reserved.