新浪微博(第五天)
来源:互联网 发布:如何倒时差 知乎 编辑:程序博客网 时间:2024/05/22 04:53
一,内容介绍
1.正则表达式的概念与使用,编写正则表达式给微博添加超链接
2. 超链接添加主题颜色,微博来源处理
3. 原微博前添加作者昵称
4. 微博来源的处理
5. 表情图片的处理显示
二,主要技术点
1. 正则表达式的开源实现:RegexKitLite 使用这个类,需要导入依赖库:libicucore.dylib
学习网站:http://www.jb51.net/tools/zhengze.html
2.在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。换句话说,正则表达式就是记录文本规则的代码,所以难点是根据元字符找到我们想要的字符串。
3.导入了WXLabel这个框架来实现富文本
三,代码实现
1.为微博内容的作者添加昵称,将原微博的内容与作者昵称拼接,同时带上@
#import "WeiboModel.h"#import "RegexKitLite.h"@implementation WeiboModel- (void)setAttributes:(NSDictionary *)dataDic { [super setAttributes:dataDic]; self.weiboId = dataDic[@"id"]; //用户数据 NSDictionary *userJson = dataDic[@"user"]; _user = [[UserModel alloc] initWithDataDic:userJson]; //原微博对象 NSDictionary *retweeted_status = dataDic[@"retweeted_status"]; if (retweeted_status != nil) { self.reWeibo = [[WeiboModel alloc] initWithDataDic:retweeted_status]; //1.为原微博的内容开头加上作者的昵称 NSString *nickName = self.reWeibo.user.screen_name; NSString *text = [NSString stringWithFormat:@"@%@:%@",nickName,self.reWeibo.text]; self.reWeibo.text = text; }2.处理微博来源的字符串,通过元字符.+在来源中找到
//2. 处理微博来源字符串 //<a href="http://app.weibo.com/t/feed/3auC5p" rel="nofollow">皮皮时光机</a> NSString *regex = @">.+<"; NSRange rang = [self.source rangeOfRegex:regex]; if (rang.location != NSNotFound) { rang.location += 1;//去掉 > rang.length -= 2;//去掉 > < 两个字符 self.source = [self.source substringWithRange:rang]; }
3.处理表情
//3. 处理表情图片 /* 1. 查找 [哈哈] 2. 查找表情名 ————> 图片名 3. 把[哈哈] 替换成 <image url = '图片名'> */ // 1 NSString *faceRegex = @"\\[\\w+\\]"; //找出表情文字 NSArray *faceItems = [self.text componentsMatchedByRegex:faceRegex]; //2 //<1> 读取emoticons.plist 表情配置文件 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoticons.plist" ofType:nil]; //配置文件是个数组 NSArray *emoticons = [NSArray arrayWithContentsOfFile:filePath]; //<2>循环、遍历所有找出来的表情名:[哈哈]、[呵呵] for (NSString *faceName in faceItems) { //<3>定义谓词条件,到emoticons.plist 中查找表情名对应的item NSString *t = [NSString stringWithFormat:@"self.chs = '%@'",faceName]; // t = self.chs = [哈哈] NSPredicate *predicate = [NSPredicate predicateWithFormat:t]; //<4>使用谓词去数组中查找过滤 NSArray *results = [emoticons filteredArrayUsingPredicate:predicate]; NSDictionary *faceDic = [results firstObject]; //<5>取得图片名 NSString *imgName = faceDic[@"png"]; if (imgName.length == 0) { continue; } //<6>构造标签 <image url = '图片名'> NSString *element = [NSString stringWithFormat:@"<image url = '%@'>",imgName]; //<7>将 [表情名] 替换成 标签:<image url = '图片名'> self.text = [self.text stringByReplacingOccurrencesOfString:faceName withString:element]; }4. 编写正则表达式,添加超链接,将WXLabel 富文本框架导入WeiboCell.h文件中,设置WXLabelDelegate代理,实现协议方法
#pragma mark - WXLabel delegate//1. 返回需要添加超链接的正则表达式- (NSString *)contentsOfRegexStringWithWXLabel:(WXLabel *)wxLabel { //<1>. @用户 NSString *regex1 = @"@[\\w]+"; //<2>. http://链接 NSString *regex2 = @"http://([a-zA-Z0-9.-_]+(/)?+)"; //<3>. #话题# NSString *regex3 = @"#.+#"; NSString *regex = [NSString stringWithFormat:@"(%@)|(%@)|(%@)",regex1,regex2,regex3]; return regex;}//2. 设置当前链接文本的颜色- (UIColor *)linkColorWithWXLabel:(WXLabel *)wxLabel { //return [UIColor blueColor]; return [[ThemeManager shareInstance] getThemeColor:@"Link_color"];}//3. 设置当前文本手指经过的颜色- (UIColor *)passColorWithWXLabel:(WXLabel *)wxLabel { return [UIColor lightGrayColor];}//4.手指离开当前超链接文本响应的协议方法- (void)toucheEndWXLabel:(WXLabel *)wxLabel withContext:(NSString *)context { NSLog(@"%@",context);}
0 0
- 新浪微博(第五天)
- 新浪微博(第八天))
- 新浪微博(第一天)
- 新浪微博(第三天)
- 新浪微博(第七天)
- (第五天)了
- Android 第五天(上午)
- Android 第五天 (下午)
- 第五天(5道)
- 移植第五天(文件系统)
- 第五天(Mysql+Php)
- 第五天
- 第五天
- 第五天
- 第五天
- 第五天
- 第五天
- 第五天
- HDU 5117 Fluorescent(2014 ACM/ICPC 北京赛区现场赛)
- ThreadLocal
- 嵌入式Linux启动过程分析2-overview
- 计算机组成原理2
- 数据挖掘回顾七:回归算法之 线性回归
- 新浪微博(第五天)
- MySQL 数据库开发的 36 条军规
- 嵌入式Linux启动过程分析3-bootloader
- Protobuf-net
- 嵌入式Linux启动过程分析4-u-boot-1step-start.S
- 音乐app的分析(例:QQ音乐)
- 信息图:A轮、B轮、天使轮,VC、PE、GP、LP
- 嵌入式Linux启动过程分析5-u-boot-1step-word
- Android 自定义Spinner字体、颜色、大小