新浪微博-首页的普通文本转换富文本

主要是利用正则表达式,匹配是否有【】,但是正则表达式只能反别获取匹配的字符串,和不匹配的字符串,需要匹配两次,需要把每一次取出来的的字符串和范围和是否是表情的bool值,这时候自定义一个模型

@interface LSRegexResult : NSObject
//匹配到的字符串
@property (nonatomic, copy) NSString *string;
//匹配到的范围
@property(nonatomic,assign) NSRange range;
//是否是表情
@property(nonatomic,assign,getter=isEmotion) BOOL emotion;

所以这时候需要一个数组把遍历取到的字符串添加到数组中,然后按照range.location进行排序,创建可变的富文本,然后在遍历排序完的数组,根据LSRegexResult.isEmotion判断是插入什么,是则创建自定义的LSTextAttachment(NSTextAttachment)即显示图片表情的配件,然后根据此配件创建一个富文本添加到可变富文本中,如果是字符串需要匹配@ 话题#话题# 还有超链接 ,需要利用正则表达式匹配,然后创建一个富文本,并添加属性,并让其蓝色显示,还需要把对应的字符串添加到属性中,方便在进行事件处理时判断是否是一个链接,还方便取出对应的内容,然后添加到可变富文本中

遍历普通文本,对数组排序

-(NSArray*)createAttributedArrayWithText:(NSString *)text
{
    NSMutableArray *results=[NSMutableArray array];
    NSString *regex=@"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";
    //匹配表情
    [text enumerateStringsMatchedByRegex:regex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        LSRegexResult *regexResult=[[LSRegexResult alloc]init];
        regexResult.string=*capturedStrings;
//        NSLog(@"string======%@",*capturedStrings);
        regexResult.range=*capturedRanges;
        regexResult.emotion=YES;
        [results addObject:regexResult];
    }];
    //匹配非表情
    [text enumerateStringsSeparatedByRegex:regex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        LSRegexResult *regexResult=[[LSRegexResult alloc]init];
        regexResult.string=*capturedStrings;
        regexResult.range=*capturedRanges;
        regexResult.emotion=NO;
        [results addObject:regexResult];
    }];
    //对数组进行排序
    [results sortUsingComparator:^NSComparisonResult(LSRegexResult * obj1, LSRegexResult* obj2) {
        int loc1=obj1.range.location;
        int loc2=obj2.range.location;
        return  [ @(loc1) compare:@(loc2)];
    }];
    return results;
}
遍历排序后的数组,匹配@ 话题等,

-(NSMutableAttributedString*)attributedStringWithText:(NSString*)text
{
    //匹配字符串
    NSArray *results= [self createAttributedArrayWithText:text];
    //遍历数组
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]init];
    [results enumerateObjectsUsingBlock:^(LSRegexResult * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LSEmotion *emotion=nil;
        if (obj.isEmotion) {//是表情
            emotion=[LSEmotionTool emotionWithText:obj.string];//是一个工具类就是遍历表情数组,直到描述和此描述相等
        }
        if (emotion) {//找到对应表情 <span style="background-color: rgb(240, 240, 240);">这里为什么需要判断因为有可能新浪返回的表情,本地没有</span>
            
            LSTextAttachment *atach=[[LSTextAttachment alloc]init];
            atach.emotion=emotion;
            atach.bounds=CGRectMake(0, -3, LSTextFont.lineHeight, LSTextFont.lineHeight);
            NSAttributedString *att=[NSAttributedString attributedStringWithAttachment:atach];
            [attributedString appendAttributedString:att];
            
        }else {//没找到对应表情
            NSMutableAttributedString *subStr=[[NSMutableAttributedString alloc]initWithString:obj.string];
            //匹配#话题#
            NSString *trendRegex = @"#[a-zA-Z0-9\\u4e00-\\u9fa5]+#";
            [obj.string enumerateStringsMatchedByRegex:trendRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
                [subStr addAttribute:NSForegroundColorAttributeName value:LSHighTextColor range:*capturedRanges];
                [subStr addAttribute:LSHighLink value:*capturedStrings range:*capturedRanges];
            }];
            //匹配超链接
            NSString *httpRegex = @"http(s)?://([a-zA-Z|\\d]+\\.)+[a-zA-Z|\\d]+(/[a-zA-Z|\\d|\\-|\\+|_./?%&=]*)?";
            [obj.string enumerateStringsMatchedByRegex:httpRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
                [subStr addAttribute:NSForegroundColorAttributeName value:LSHighTextColor range:*capturedRanges];
                [subStr addAttribute:LSHighLink value:*capturedStrings range:*capturedRanges];
            }];
            //匹配@
            NSString *mentionRegex = @"@[a-zA-Z0-9\\u4e00-\\u9fa5\\-_]+";
            [obj.string enumerateStringsMatchedByRegex:mentionRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
                [subStr addAttribute:NSForegroundColorAttributeName value:LSHighTextColor range:*capturedRanges];
                [subStr addAttribute:LSHighLink value:*capturedStrings range:*capturedRanges];
            }];
            [attributedString  appendAttributedString:subStr];
        }
        
    }];
    //设置字体
    [attributedString addAttribute:NSFontAttributeName value:LSTextFont range:NSMakeRange(0, attributedString.length)];
    return attributedString;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值