5

Is there a simple way to split a NSAttributedString so I get only the last 50 or so lines?

NSMutableAttributedString *resultString = [receiveView.attributedText mutableCopy];
[resultString appendAttributedString:[ansiEscapeHelper attributedStringWithANSIEscapedString:message]];
if ([[resultString.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count]>50) {
    //resultString = [resultString getLastFiftyLines];
}
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
David Karlsson
  • 9,396
  • 9
  • 58
  • 103
  • 1
    Are you asking about lines or chars? ([resultString length]>50) - about chars, not lines – Tala Jul 01 '13 at 19:49
  • I guess both works, but i would prefer the rows. – David Karlsson Jul 01 '13 at 20:05
  • Rows in a string are identified according font size + ui element width + word wrap. If 50 chars works for, don't make it too complicated and use substring as in my answer – Tala Jul 01 '13 at 20:07
  • This is an attributed string with ansi coding and lots of '\n' characters. so your line definition is incorrect in this case. Meaning some modifications to Justins answer might work better for me – David Karlsson Jul 01 '13 at 20:13
  • Substring what you guess is maybe 75 lines off the end of the text, use `componentsSeparatedBy...` to split the lines, then take the last 50 in the resulting array. If not 50, double your guess and try again. (Be sure to throw away the very first line in the resulting array as it will likely be a partial.) – Hot Licks Jul 01 '13 at 21:39

2 Answers2

3

is there a simple way to split a NSAttributedString so i get only the last 50 or so lines?

No. You will have to request the string and determine the range you are interested in, then create a new NSAttributedString representation derived from the source using an API such as - [NSAttributedString attributedSubstringFromRange:]:

- (NSAttributedString *)lastFiftyLinesOfAttributedString:(NSAttributedString *)pInput
{
  NSString * string = pInput.string;
  NSRange rangeOfInterest = ...determine the last 50 lines in "string"...;
 return [pInput attributedSubstringFromRange:rangeOfInterest];
}
justin
  • 104,054
  • 14
  • 179
  • 226
  • That does not sound very hard... I guess i could determine the last 50 rows from: [[resultString.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count] But how do I make an NSRange out of that... – David Karlsson Jul 01 '13 at 20:21
  • @DavidKarlsson using that API, you could add up the `-length` of the last 50 strings returned from `-componentsSeparatedByCharactersInSet:` and add 1 for each newline (omitted by `-componentsSeparatedByCharactersInSet:`). the sum is the *length* of the string of interest (e.g. the last 234 characters), so the `NSRange` would be `{string.length-sum /* location */, sum /* length */ }`. – justin Jul 01 '13 at 21:19
2

You can use substring method of AttributedString:

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

NSMakeRange - 0 tells us where to start and 50 is length of substring

Tala
  • 8,888
  • 5
  • 34
  • 38