环信IM集成 —— 聊天界面URL颜色展示

来源:互联网 发布:思科网络技术学院答案 编辑:程序博客网 时间:2024/06/11 05:51

当前环境:3.0环境
需要两步操作:

1 . 需要在初始化时进行一步处理,要求如果是URL字符串,则要求将颜色进行调整

所在文件 : EaseMessageCell.m
在方法中添加如下方法操作

- (void)setMessageTextColor:(UIColor *)messageTextColor{    _messageTextColor = messageTextColor;    if (_bubbleView.textLabel) {        _bubbleView.textLabel.textColor = _messageTextColor;    }    NSLog(@"当前的属性字符串:%@",_bubbleView.textLabel.attributedText);    [self highlightLinks];}

自定义方法(只解析了全部字符串为网址的情况)

- (void)highlightLinks {    NSMutableAttributedString * aInputText = [_bubbleView.textLabel.attributedText mutableCopy];    NSString *string = aInputText.string;    if ([string hasPrefix:@"http://"] || [string hasPrefix:@"https://"]) {        [aInputText addAttribute:NSForegroundColorAttributeName value:kColorRGB(239, 52, 12) range:NSMakeRange(0, string.length)];  // 添加下划线    }    _bubbleView.textLabel.attributedText = aInputText;}

2 . 在EaseEmotionEscape.m中添加条件判断是否为网址

- (NSAttributedString *) attStringFromTextForChatting:(NSString *) aInputText textFont:(UIFont*)font{    NSMutableAttributedString * string = [self attributtedStringFromText:aInputText];    if ([aInputText hasPrefix:@"http://"] || [aInputText hasPrefix:@"https://"]) {        [string addAttribute:NSForegroundColorAttributeName value:kColorRGB(239, 52, 12) range:NSMakeRange(0, string.length)];  // 添加下划线//        [string addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, string.length)];    }    if (font) {        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];    }    return string;}
0 0
原创粉丝点击