Is it possible to extract the string content of a specific line in a UITextView? Thanks.
A:
Just add a category to the UITextView. Note that is is pretty expensive, but definitely the simplest way to do it. Also it doesn't work for other kinds of newlines, like "\r", but you could add that.
// UITextView+ContentsOfLine.h
@interface UITextView (ContentsOfLine)
- (NSString*)contentsOfLine:(NSInteger)line;
@end
// UITextView+ContentsOfLine.m
@implementation UITextView (ContentsOfLine)
- (NSString*)contentsOfLine:(NSInteger)line {
NSArray* contents = [[self text] componentsSeparatedByString:@"\n"];
if( [contents length] > line ) {
return [contents objectAtIndex:line];
}
return nil;
}
@end
Steven Canfield
2010-03-15 20:47:36
Hi, thanks. I tried this but it does not work as UITextView does not seem to autoinsert \n when wrapping words.
Run Loop
2010-03-16 04:43:53
A:
Finding the content of a line in UITextView can only be solved through an extremely expensive process of iteration using the "sizeWithFont" methods. It could be done for a static page, but is not suitable for content that appears in a scrollview or tableview.
Run Loop
2010-03-27 05:10:47