Extract the last 50 rows from NSAttributedString

Is there an easy way to split NSAttributedString, so I only get the last 50 lines?

NSMutableAttributedString *resultString = [receiveView.attributedText mutableCopy]; [resultString appendAttributedString:[ansiEscapeHelper attributedStringWithANSIEscapedString:message]]; if ([[resultString.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count]>50) { //resultString = [resultString getLastFiftyLines]; } 
+4
source share
2 answers

Is there an easy way to split NSAttributedString, so I only get the last 50 lines?

No. You will need to query the string and determine the range of interest to you, and then create a new NSAttributedString obtained from the source using the API, for example - [NSAttributedString attributedSubstringFromRange:] :

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

You can use the AttributedString substring method:

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

NSMakeRange - 0 indicates where to start, and 50 - substring length

+2
source

Source: https://habr.com/ru/post/1489148/


All Articles