新浪微博-首页的普通文本转换富文本
来源:互联网 发布:银行大数据分析现状 编辑:程序博客网 时间:2024/04/30 05:40
主要是利用正则表达式,匹配是否有【】,但是正则表达式只能反别获取匹配的字符串,和不匹配的字符串,需要匹配两次,需要把每一次取出来的的字符串和范围和是否是表情的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
- 新浪微博-首页的普通文本转换富文本
- 新浪微博-首页富文本的事件处理
- js普通文本转富文本
- Lable的富文本
- Lable的富文本
- 富文本的使用
- 富文本
- 富文本
- 富文本
- 富文本
- 富文本
- 富文本
- 富文本
- 富文本
- 我的富文本编辑器
- 常用的富文本标签
- UILabel 简单的富文本
- NSMutableAttributedString富文本的实现
- CSS中英文字体
- 利用有限自动机进行字符串匹配
- Linux下安装PHP
- ajax提交Form
- Android阴影布局开源框架
- 新浪微博-首页的普通文本转换富文本
- 靠谱的爱代驾司机
- java的equals、hashcode和Clone方法
- 网易新闻
- [MySQL FAQ]系列 — 从MyISAM转到InnoDB需要注意什
- Servlet、Request、Response、filter、listener
- android异常
- Lambda表达式详解
- XJOI 迷你火车头