开发者

Content of a specific line in UITextView

Is it possible to extract the string content of a specific line in a 开发者_开发百科UITextView? Thanks.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜