码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之— NSRegularExpression正则分割字符串

时间:2014-12-04 20:05:01      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   os   sp   for   on   div   bs   

社交化越来为社会所接受,在做社会化产品时,在回复或者转发时会@他人 展示出来,点击@user 跳转到该用户的介绍页面,这里首先要制定恰当的标签,比如:<at href=\"jecky://profile/239\">iJecky</at>,然后客户端拿到文本时,要用到NSRegularExpression分割string,下面以富文本OHAttributedLabel来演示:

说明:本文中用到的contentLabel为OHAttributedLabel类型

示例:

NSString  *atstring = @"测试啊测试啊 <at href=\"jecky://profile/239\">iJecky</at> 测试啊测试啊";


首先分割:

    NSMutableString *resultStr = [NSMutableString stringWithCapacity:0];

    NSString *pattern = @"<at href=\"(.*?)\">(.*?)</at>";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

    NSArray *matches = [regex matchesInString:atstring options:0 range:NSMakeRange(0, atstring.length)];

    NSUInteger lastIdx = 0;

    for (NSTextCheckingResult* match in matches)

    {

        NSRange range = match.range;

        if (range.location > lastIdx)

        {

            NSString  *temp = [atstring substringWithRange:NSMakeRange(lastIdx, range.location - lastIdx)];

            [resultStr appendString:temp];

        }

        NSString *link = [atstring substringWithRange:[match rangeAtIndex:1]];

        NSString *text = [atstring substringWithRange:[match rangeAtIndex:2]];

        NSString *atName = [NSString stringWithFormat:@"@%@",text];

        [resultStr appendString:atName];

        [markDic setObject:link forKey:atName];

        lastIdx = range.location + range.length;

    }

    

    if (lastIdx < atstring.length)

    {

        NSString  *temp = [atstring substringFromIndex:lastIdx];

        [resultStr appendString:temp];

    }

    

    self.contentLabel.text = resultStr;


然后展示到contentLabel上

    NSRegularExpression* userRegex = [NSRegularExpression regularExpressionWithPattern:@"\\B@\\w+" options:0 error:nil];

    NSMutableAttributedString* mas = [self.contentLabel.attributedText mutableCopy];

    

    [userRegex enumerateMatchesInString:self.contentLabel.text options:0

                                  range:NSMakeRange(0,self.contentLabel.text.length)

                             usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

                                 NSString  *auser = [self.contentLabel.text substringWithRange:match.range];       

                                 NSString  *atlink = [markDic objectForKey:auser];

                                 if (atlink) {

                                     [mas setLink:[NSURL URLWithString:[markDic objectForKey:auser]] range:match.range]; // add it

                                 }

                             }];

    

    OHParagraphStyle* para = [OHParagraphStyle defaultParagraphStyle];

    para.firstLineHeadIndent = 0;

    para.headIndent = 5;

    para.tailIndent = -5;

    para.textAlignment = kCTTextAlignmentLeft;

    [mas setParagraphStyle:para];

    [OHASBasicMarkupParser processMarkupInAttributedString:mas];


    self.contentLabel.attributedText = mas;





iOS之— NSRegularExpression正则分割字符串

标签:style   io   ar   os   sp   for   on   div   bs   

原文地址:http://blog.csdn.net/pjk1129/article/details/41727603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!