zoukankan      html  css  js  c++  java
  • 新浪微博客户端(55)-高亮显示微博内容中的昵称,话题,超链接

    DJStatus.h

    #import <Foundation/Foundation.h>
    
    @class DJUser;
    
    /** 微博 */
    @interface DJStatus : NSObject
    
    /** 微博id */
    @property (nonatomic,copy) NSString *idstr;
    /** 微博内容 */
    @property (nonatomic,copy) NSString *text;
    /** 微博内容(带属性) */
    @property (nonatomic,copy) NSAttributedString *attributedText;
    /** 微博关联用户 */
    @property (nonatomic,strong) DJUser *user;
    /** 发布日期 */
    @property (nonatomic,copy) NSString *created_at;
    /** 来源 */
    @property (nonatomic,copy) NSString *source;
    /** 配图 */
    @property (nonatomic,strong) NSArray *pic_urls;
    /** 转发微博 */
    @property (nonatomic,strong) DJStatus *retweeted_status;
    /** 转发微博(带属性) */
    @property (nonatomic,copy) NSAttributedString *retweetedAttributedText;
    /** 转发数 */
    @property (nonatomic,assign) int reposts_count;
    /** 评论数 */
    @property (nonatomic,assign) int comments_count;
    /** 表态数 */
    @property (nonatomic,assign) int attitudes_count;
    
    
    @end

    DJStatus.m

    // 设置带属性的文本内容
    - (void)setText:(NSString *)text {
    
        _text = text;
        
        // 将微博内容文本转换为带属性的微博内容文本
        _attributedText = [self attributedTextWithText:text];
    
    }
    
    
    
    - (void)setRetweeted_status:(DJStatus *)retweeted_status {
    
        _retweeted_status = retweeted_status;
        
        DJUser *retwetedUser = retweeted_status.user;
        NSString *retweetedText = [NSString stringWithFormat:@"@%@: %@",retwetedUser.name,retweeted_status.text];
        
        _retweetedAttributedText = [self attributedTextWithText:retweetedText];
        
    }
    
    
    
    /** 普通文本->属性文本 */
    - (NSAttributedString *)attributedTextWithText:(NSString *)text {
        
        // 表情的规则
        NSString *emotionPattern = @"\[[0-9a-zA-Z\u4e00-\u9fa5]+\]";
        // @的规则
        NSString *atPattern = @"@[0-9a-zA-Z\u4e00-\u9fa5-_]+";
        // #话题#的规则
        NSString *topicPattern = @"#[0-9a-zA-Z\u4e00-\u9fa5]+#";
        // url链接的规则
        NSString *urlPattern = @"\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))";
        NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@", emotionPattern, atPattern, topicPattern, urlPattern];
        
        // 利用当前文本生成attributedText
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
        
        // 遍历所有特殊字符串
        [text enumerateStringsMatchedByRegex:pattern usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
            // !注意: 此处block里回传的是NSRange的指针变量,需要通过*capturedRanges取出对应的NSRange
            [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:*capturedRanges];
            
        }];
        
        return attributedText;
        
    }

    DJStatusCell.m

    - (void)setStatusFrame:(DJStatusCellFrame *)statusFrame {
          // 微博内容
        self.contentLabel.frame = statusFrame.contentLabelF;
    //    self.contentLabel.text = status.text;
        self.contentLabel.attributedText = status.attributedText;
    
     /* 转发微博 */
        DJStatus *retweetedStatus = status.retweeted_status;
        if (retweetedStatus) { // 如果有转发微博
            self.retweetView.hidden = NO;
    //        DJUser *retwetedUser = retweetedStatus.user;
            
            // 转发微博内容
            self.retweetContentLabel.frame = statusFrame.retweetContentLabelF;
    //        self.retweetContentLabel.text = [NSString stringWithFormat:@"@%@: %@",retwetedUser.name,retweetedStatus.text];
            self.retweetContentLabel.attributedText = status.retweetedAttributedText;
            
            // 转发微博相册
            if (retweetedStatus.pic_urls.count) { // 如果有微博相册
                self.retweetPhotosView.hidden = NO;
                self.retweetPhotosView.frame = statusFrame.retweetPhotosViewF;
                self.retweetPhotosView.photos = retweetedStatus.pic_urls;
            } else { // 没有微博配图
                self.retweetPhotosView.hidden = YES;
            }
            // 转发微博整体
            self.retweetView.frame = statusFrame.retweetViewF;
            
        } else { // 如果没有转发微博
            self.retweetView.hidden = YES;
        }
      
    
    }

    集成RegexKitLite框架

    1. 添加RegexKitLite的库文件到项目:

    2. 如果此时执行Command+B编译,Xcode会报错:

    3. 错误的原因是RegexKitLite框架是非ARC的代码,因此我们需要在Xcode中指定RegexKitLite为非ARC

    4. 完成上述操作后再次编译,发现还是报错,这是因为RegexKitLite依赖于libicucore.dylib这个动态库,因此我们在Xcode中添加这个库

    5. 完成上述操作后,再次编译,同时在需要使用这个库的.m文件中导入RegexKitLite的头文件“RegexKitLite.h”,发现已经可以正常使用了

    最终效果:

  • 相关阅读:
    数据库从别的数据库查询一张表在插入到新的数据库里面
    html5 学习随笔 1
    .net MVC 学习笔记 (一)
    Html5 本地存储
    .net MVC 学习笔记 (二)
    蝙蝠侠解救罗宾的问题
    求职的第一面Harman
    求职第七面——烽火通讯
    求职的第二面—Samsung
    求职第六面——瑞星微电子
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/6165875.html
Copyright © 2011-2022 走看看