环信版本:
3.3.2
相关代码放到github上了:https://github.com/fcgeek/myEaseMessage
以前的版本是直接支持的,不知道从什么时候起,不支持了。应该是新的一波人重构了代码,然后还没有处理到链接支持吧。
Demo地址链接: https://pan.baidu.com/s/1qYx3ypA 密码: vv57
原料
步奏
1.导入TTTAttributedLabel到EaseUI
,详情见它的github;
2.EaseUI
文件夹中EaseBubbleView.h
的textLabel
类型替换成TTTAttributedLabel
#import <UIKit/UIKit.h>
@class TTTAttributedLabel; //Line 10
//text views
@property (strong, nonatomic) TTTAttributedLabel *textLabel; //Line 42
3.EaseBubbleView+Text.m
中初始化textLabel
#import "TTTAttributedLabel.h" //Line 14
- (void)setupTextBubbleView //Line 45
{
self.textLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.textLabel.numberOfLines = 0;
self.textLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
//`画重点`: 支持交互动作
self.backgroundImageView.userInteractionEnabled = YES;
[self.backgroundImageView addSubview:self.textLabel];
[self _setupTextBubbleConstraints];
}
4.传递TTTAttributedLabelDelegate
"EaseMessageCell.h"的EaseMessageCellDelegate
添加�点击链接方法
#import "TTTAttributedLabel.h" //Line 19
@protocol EaseMessageCellDelegate <NSObject>
@optional
/*
* 链接点击回调
*
* @param 链接
*/
- (void)messageCell:(EaseMessageCell *)cell didSelectLinkWithURL:(NSURL *)url;
/// ....忽略代码
@end
"EaseMessageCell.m" 实现点击链接传递Delegate
@interface EaseMessageCell()<TTTAttributedLabelDelegate> // Line 41
// `画重点`:设置delegate
_bubbleView.textLabel.delegate = self;//Line 173
// `画重点`:很多朋友说点击没回调,问题在这里;
tapRecognizer.cancelsTouchesInView = NO; //Line 224
// `画重点`:注释了环信自己的样式,`BubbleView`计算部分会存在问题,这个自己想办法处理吧
// 提供两个思路 1、`TTTAttributedLabel`使用环信的;2、环信`BubbleView`使用`TTTAttributedLabel`的样式来计算
_bubbleView.textLabel.text = model.text;//.attributedText = [[EaseEmotionEscape sharedInstance] attStringFromTextForChatting:model.text textFont:self.messageTextFont];
#pragma mark - TTTAttributedLabelDelegate //Line 883
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([_delegate respondsToSelector:@selector(messageCell:didSelectLinkWithURL:)]) {
[_delegate messageCell:self didSelectLinkWithURL:url];
}
}
5.实现EaseMessageCell
点击链接方法
"EaseMessageViewController.h"添加EaseMessageViewControllerDelegate
点击链接跳转方法
/**
* 点击cell链接时
*/
- (void)messageViewController:(EaseMessageViewController *)viewController
didSelectLinkWithURL:(NSURL *)url; //Line 47
6.EaseMessageViewController.m
实现EaseMessageCellDelegate
点击链接方法
#pragma mark - EaseMessageCellDelegate Line 1186
- (void)messageCell:(EaseMessageCell *)cell didSelectLinkWithURL:(NSURL *)url {
if (self.delegate && [self.delegate respondsToSelector:@selector(messageViewController:didSelectLinkWithURL:)]) {
[self.delegate messageViewController:self didSelectLinkWithURL:url];
} else {
[[UIApplication sharedApplication] openURL:url];
}
}