What is the most efficient way to pull the first row without spaces from an NSTextView?
For example, if the text:
\n
\n
\n
This is the text I want \n
\n
Foo bar \n
\n
The result is "This is the text I want."
Here is what I have:
NSString *content = self.textView.textStorage.string;
NSInteger len = [content length];
NSInteger i = 0;
while (i < len && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[content characterAtIndex:i]]) {
i++;
}
while (i < len && ![[NSCharacterSet newlineCharacterSet] characterIsMember:[content characterAtIndex:i]]) {
i++;
}
NSString *resultWithWhitespace = [content substringToIndex:i];
NSString *result = [resultWithWhitespace stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Is there a better, more efficient way?
I am thinking of putting this in the -textStorageDidProcessEditing: NSTextStorageDelegate method so that I can get it when editing text. That is why I would like the method to be as efficient as possible.
source
share