//表情处理 导入框架 #import "RegexKitLite.h" - (void)setText:(NSString *)text{ //[表情文件] -> 对应的图片名 -> <image url = '图片名'> //[威武] 谓词 //1.加载表情配置文件 NSString *faceFile = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"]; NSArray *faceItem = [NSArray arrayWithContentsOfFile:faceFile]; //2.找到微博内容中的[表情文件] NSString *faceRegex = @"\\[\\w+\\]"; //3.获取字符串中指定的字符 返回数组 NSArray *faceArray = [text componentsMatchedByRegex:faceRegex]; for (NSString *faceName in faceArray) { //1.@"[兔子]",-> 001.png NSString *t = [NSString stringWithFormat:@"self.chs='%@'",faceName]; NSPredicate *predicate = [NSPredicate predicateWithFormat:t]; NSArray *items = [faceItem filteredArrayUsingPredicate:predicate]; //判断如果有元素符合 if(items.count > 0) { NSDictionary *faceDic = items[0]; //取得对应的图片 NSString *imgName = faceDic[@"png"]; //2.图片的名字 -> <image url = '图片名'> NSString *replaceStr = [NSString stringWithFormat:@"<image url = '%@'>",imgName]; //3.替换字符串 _text = [text stringByReplacingOccurrencesOfString:faceName withString:replaceStr]; } } }
// 微博内容
self.text.text = sModel.text;
//使用方法计算高度
float height = [WXLabelgetTextHeight:TextFontwidth:contentLabelMaxW text:status.text];
链接: http://pan.baidu.com/s/1sjyuDu1 密码: zk7m
表情包:链接: http://pan.baidu.com/s/1BFMX4 密码: 5dyj
注意:使用WXLable后有报错的话 注释掉这些
// if (image) {
// CGRect imageDrawRect;
//#warning 设置图片的大小
// imageDrawRect.size = CGSizeMake(self.font.pointSize * 1.2,self.font.pointSize * 1.2);
// imageDrawRect.origin.x = runRect.origin.x + lineOrigin.x;
// imageDrawRect.origin.y = lineOrigin.y - self.font.pointSize * .2;
// CGContextDrawImage(context,imageDrawRect,image.CGImage);
// // imageDrawRect.size = CGSizeMake(image.size.height,image.size.height);
// // imageDrawRect.origin.x = runRect.origin.x + lineOrigin.x;
// // imageDrawRect.origin.y = lineOrigin.y - 8;
// // CGContextDrawImage(context,image.CGImage);
// }
原文链接:https://www.f2er.com/regex/360122.html