環(huán)信聊天界面,如果是超鏈接,目前版本是不支持打開的,之前是有版本支持的,這個屬于UI層,需要我們自己處理;
思路
- 1.檢測聊天文本,如果是超鏈接,添加下劃線-->
UILabel
富文本屬性 - 2.監(jiān)聽文本點擊事件,如果是超鏈接打開-->
chatViewController
代理方法
具體代碼
1.EaseMessageCell添加分類,在分類里判斷是否是超鏈接,是超鏈接加下劃線
@implementation EaseMessageCell (DXLinkClick)
-(void)addLinks:(NSString*)str toLabel:(UILabel*)label
{
NSMutableAttributedString*strMutable=[[NSMutableAttributedString alloc]initWithString:str];
[strMutable addAttribute:NSFontAttributeName value:label.font range:NSMakeRange(0, str.length)];
[strMutable addAttribute:NSForegroundColorAttributeName value:label.textColor range:NSMakeRange(0, str.length)];
NSDataDetector*detect=[[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];
NSArray*matches=[detect matchesInString:str options:0 range:NSMakeRange(0, str.length)];
for(NSTextCheckingResult*result in matches)
{
if (result.resultType==NSTextCheckingTypeLink) {
[strMutable addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:result.range ];
}
}
if ([matches count]>0) {
label.attributedText=strMutable;
}
}
@end
2.在自定義的EaseMessageCell
里,在模型的set
方法里調(diào)用分類方法
- (void)setModel:(id<IMessageModel>)model
{
_model = model;
if ([self respondsToSelector:@selector(isCustomBubbleView:)] && [self isCustomBubbleView:model]) {
[self setCustomModel:model];
} else {
switch (model.bodyType) {
case eMessageBodyType_Text:
{
_bubbleView.textLabel.text = model.text;
[self addLinks:model.text toLabel:_bubbleView.textLabel];
}
break;
// 以下省略好多字
3.在chatViewController中,重寫代理方法
- (BOOL)messageViewController:(EaseMessageViewController *)viewController didSelectMessageModel:(id<IMessageModel>)messageModel
{
BOOL flag = NO;
NSString*str=[messageModel text];
NSDataDetector*detector=[[NSDataDetector alloc]initWithTypes:NSTextCheckingTypeLink error:nil];
NSArray*array=[detector matchesInString:str options:0 range:NSMakeRange(0, str.length)];
if([array count]>0)
{
// 這里用Safari打開了,也可以自定義在app內(nèi)打開
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:resultStr]];
}
return flag;
}