這樣的。
找了很多資料备埃,發(fā)現(xiàn)都得用圖文混排的方式去處理姓惑,其中很多的表情字符串是[大哭]這樣的形式,然后對于:sad:這樣的形式就又有點懵逼了按脚,后來用正則的方式去處理了于毙,發(fā)現(xiàn)確實好用,同時也處理了tableviewCell的自適應高度問題辅搬。接下來和大家分享一下唯沮。
采用的是三方MLLabel,在pod file中添加pod'MLLabel' ,用終端 pod install,將三方導入自己的工程中堪遂。
準備工作做好后介蛉,開始進行編碼
1.在ViewController中導入
#import <MLLabel/NSString+MLExpression.h>
創(chuàng)建全局屬性
@property (nonatomic, strong) NSMutableArray *expressionData;
@property(nonatomic, strong)NSMutableArray *contentArray;
@property(nonatomic, strong)UITableView *tableview;
- (void)viewDidLoad {
_contentArray = [NSMutableArray arrayWithObjects:@"對待好東西一定不能猶豫,早起搶了張秒殺票 :twisted: :twisted:",@"終于又等到北京的大課溶褪,好激動 :mrgreen: :mrgreen: :mrgreen: 這次和公司小伙伴準備買成團票~ :smile: :smile:",@"昨天下午部門臨時開了一個緊急的小會甘耿,老總說:聽說起點學院在北京又開總監(jiān)課程呢!上次因為公司在趕項目沒有辦法 :cry: 竿滨,這次一定要報名佳恬,我們竟然想做大做好,就一定要有學習的精神于游,明天大家定好鬧鈴毁葱,搶票N",@":razz: :roll: :roll: 報名走一個,修煉贰剥,修煉倾剿,我就要究極變化了~~比卡~比卡~比卡丘",@"看到同事了哈哈哈 :lol:",@"我是得加他們微信才能發(fā)圖片吧?申請加好友也沒人回復鞍龀伞前痘?這是為什么呢? :arrow:",@"回復Uncle Wang :什么時候可以加上呢 :arrow:", @"回復lisa991 :小助手寶寶的微信已經(jīng)被加爆了担忧,請稍安勿躁芹缔,摸摸大 :lol:",@"居然沒人搭理~嚴重打擊了用戶的信任~哼 :mad: :mad: :mad: :mad: :mad: :mad:",@"終于等到你~ :razz: :razz: :razz:",@"還好有直播,要不然就要遺憾慘了~好開心~~~ :mrgreen: :mrgreen: :mrgreen:",@"哦啦啦瓶盛,這次搶到票啦最欠,去高逼格的現(xiàn)場示罗,啦啦啦,好開心 :grin: :grin:", @"特么周六上班 :mad:",@"從技術(shù)架構(gòu)角度看芝硬,傳統(tǒng)垂直架構(gòu)的特點是本地API接口調(diào)用蚜点,不存在業(yè)務的拆分和互相調(diào)用,使用到什么功能就本地開發(fā)拌阴,非常方便绍绘,不需要過度依賴于其它功能模塊;從考核角度來看迟赃,共享很難推行脯倒。開發(fā)只需要對自己開發(fā)的模塊交付質(zhì)量負責,沒有義務為他人提供并維護公共類庫捺氢,這個非常耗費成本藻丢; 時間依賴很難把控:對于公共類庫的使用者而言,依賴別人提供此功能摄乒,但是功能提供者可能有更重要的事情在做悠反,提供時間無法滿足使用者。與其坐等別人提供馍佑,還不如自己開發(fā)效率高斋否;",nil];
//用正則表達式匹配表情字符串
MLExpression *exp = [MLExpression expressionWithRegex:@"\\\\:[a-zA-Z0-9\\\\u4e00-\\\\u9fa5\\\\?\\\\!]+\\\\:" plistName:@"Expression" bundleName:@"Emotion"];
[MLExpressionManager expressionAttributedStringsWithStrings:_contentArray expression:exp callback:^(NSArray *result) {
self.expressionData = [NSMutableArray arrayWithArray:result];
[_tableview reloadData];
}];
}
匹配方法中plistName和bundleName是你創(chuàng)建的兩個文件。表情字符串是和本地的表情圖片相對應的拭荤,所以應該先把本地的表情圖片放到bundle中茵臭,然后再寫個plist文件,將表情字符串和表情圖片對應上舅世。bundle和plist文件的創(chuàng)建旦委,是在iOS->Resource中。如圖:
創(chuàng)建好的兩個文件如圖:
我們用tableview去顯示相應的內(nèi)容雏亚,首先缨硝,自定義tableviewcell。
#import <UIKit/UIKit.h>
#import <MLLabel/MLLinkLabel.h>
@interface ContentCell : UITableViewCell
+ (CGFloat)heightForExpressionText:(NSAttributedString*)expressionText width:(CGFloat)width;//計算高度
@property (strong, nonatomic)MLLinkLabel *textlabel;
@end
ContentCell.m的相關(guān)實現(xiàn)
#import "ContentCell.h"
@implementation ContentCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:self.textlabel];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.textlabel.frame = CGRectMake(13, 0, self.contentView.frame.size.width - 26, self.contentView.frame.size.height);
}
#pragma mark - getter
- (MLLinkLabel *)textlabel
{
if (!_textlabel) {
_textlabel = [MLLinkLabel new];
_textlabel.textColor = [UIColor colorWithRed:0.51 green:0.51 blue:0.51 alpha:1];;
// _textlabel.backgroundColor = [UIColor colorWithRed:0.137 green:0.780 blue:0.118 alpha:1.000];
_textlabel.font = [UIFont systemFontOfSize:14.0f];
_textlabel.numberOfLines = 0;
_textlabel.lineSpacing = 2.0f;//行間距
//_textlabel.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.51 green:0.51 blue:0.51 alpha:1]};//帶鏈接時鏈接的顏色
}
return _textlabel;
}
#pragma mark - height
static MLLinkLabel * kProtypeLabel() {
static MLLinkLabel *_protypeLabel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_protypeLabel = [MLLinkLabel new];
_protypeLabel.font = [UIFont systemFontOfSize:14.0f];
_protypeLabel.numberOfLines = 0;
// _protypeLabel.textInsets = UIEdgeInsetsMake(10, 10, 10, 10);
// _protypeLabel.lineHeightMultiple = 1.1f;
_protypeLabel.lineSpacing = 2.0f;
});
return _protypeLabel;
}
+ (CGFloat)heightForExpressionText:(NSAttributedString*)expressionText width:(CGFloat)width
{
width-=13.0f;
MLLinkLabel *label = kProtypeLabel();
label.attributedText = expressionText;
return [label preferredSizeWithMaxWidth:width].height + 5.0f*2; //上下間距
}
@end
//創(chuàng)建tableview罢低,實現(xiàn)相應的代理方法
-(void)loadView{
[super loadView];
_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 24, self.view.frame.size.width, self.view.frame.size.height - 24) style:UITableViewStylePlain];
_tableview.delegate = self;
_tableview.tableFooterView = [[UIView alloc] init];
_tableview.dataSource = self;
[self.view addSubview:_tableview];
[_tableview setSeparatorInset:UIEdgeInsetsMake(0, 13, 0, 13)];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _contentArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_expressionData.count == 0) {
return 0;
}else{
CGFloat height = [ContentCell heightForExpressionText:self.expressionData[indexPath.row] width:self.view.frame.size.width-26];
return height;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[ContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if (_expressionData.count != 0) {
cell.textlabel.attributedText = self.expressionData[indexPath.row];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
這樣查辩,就可以把表情字符串轉(zhuǎn)換成對應的表情圖片了,同時也可以做到tableviewcell的自適應高度网持。結(jié)果如圖
如果有任何問題宜岛,歡迎指出或留言。