新浪微博客户端(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”,发现已经可以正常使用了

 

最终效果:

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(885)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示