新浪微博(第五天)

来源:互联网 发布:如何倒时差 知乎 编辑:程序博客网 时间:2024/05/22 04:53

    一,内容介绍

  1.正则表达式的概念与使用,编写正则表达式给微博添加超链接

  2. 超链接添加主题颜色,微博来源处理

  3. 原微博前添加作者昵称

  4. 微博来源的处理

  5. 表情图片的处理显示

    二,主要技术点

  1. 正则表达式的开源实现:RegexKitLite 使用这个类,需要导入依赖库:libicucore.dylib

  学习网站:http://www.jb51.net/tools/zhengze.html

 2.在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。换句话说,正则表达式就是记录文本规则的代码,所以难点是根据元字符找到我们想要的字符串。

表1.常用的元字符代码说明.匹配除换行符以外的任意字符\w匹配字母或数字或下划线或汉字\s匹配任意的空白符\d匹配数字\b匹配单词的开始或结束^匹配字符串的开始$匹配字符串的结束
  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
原创粉丝点击