文本中設置圖片

/*

1.創建NSTextAttachment對象

2.設置image屬性為需要展示的圖片

3.設置bounds屬性為需要展示的大小

4.通過NSTextAttachment對象生成NSAttributedString對象

5.通過字符串創建NSMutableAttributedString

6.把NSAttributedString插入到NSMutableAttributedString的任意位置

7.把NSMutableAttributedString賦值給attributedText

*/

//在文本中加入表情

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 200)];

label.numberOfLines= 0;

NSString *text =@"這是一個表情";

UIFont *font = [UIFont systemFontOfSize:30];

label.font = font;

label.text = text;

[self.view addSubview:label];

//創建NSTextAttachment的對象文本附件

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];

//創建圖片

UIImage *image = [UIImage imageNamed:@"8.jpg"];

//在文本附件上設置圖片

attachment.image= image;

//設置圖片大小圖片寬高和文字寬高一致

attachment.bounds=CGRectMake(0, 0, font.lineHeight, font.lineHeight);

//根據attachment創建屬性字符串

NSAttributedString *attr = [NSAttributedString attributedStringWithAttachment:attachment];

//創建可變屬性字符串將原本的text的內容轉化為可變的字符串

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];

//選擇插入的文字上的位置第6位

[attrStr insertAttributedString:attrat Index:6];

//替換

[attrStr replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attr];

//添加labe進行顯示

label.attributedText = attrStr;

//通過資源路徑獲取圖片進行顯示

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 200)];

label.numberOfLines= 0;

UIFont *font = [UIFont systemFontOfSize:30];

label.font= font;

[self.view addSubview:label];

NSString *text =@"今天吃飯吃了三碗飯,好開心[笑哈哈],但是胖了三斤[淚流滿面]";

//正則表達式

NSString *pattern =@"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+]";

//創建正則對象

NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

//接收根據正則表達式搜索到的結果數組

NSArray *resultArray = [regular matchesInString:text options:0 range:NSMakeRange(0, text.length)];

//創建可變屬性字符串將原本的text的內容轉化為可變的字符串

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];

//聲明偏移量屬性

__block NSIntegeroffset = 0;

//遍歷結果數組

[resultArray enumerateObjectsUsingBlock:^(NSTextCheckingResult *_Nonnullobj,NSUIntegeridx,BOOL *_Nonnullstop) {

//根據偏移量獲取實際是range

NSRange range =NSMakeRange(obj.range.location- offset, obj.range.length);

//取出表情對應的字符串

NSString *face = [text substringWithRange:obj.range];

//創建NSTextAttachment的對象文本附件

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];

//根據名字字符串去資源路徑中拿取對應的圖片

UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self pathForEmoticonWithName:face]];

//把圖片添加到文本附件上

attachment.image = image;

//可以不用設置大小,會自動適應

//attachment.bounds = CGRectMake(0, 0, font.lineHeight, font.lineHeight);

//設置文本附件的屬性字符串

NSAttributedString *att = [NSAttributedString attributedStringWithAttachment:attachment];

//圖片代替文字

[attrStr replaceCharactersInRange:range withAttributedString:att];

//更新偏移量(這樣計算下一個的obj.range.location才不會出錯)

//obj.range.length -------5-----6

//offset = obj.range.length - 1 -----4---5

//offset = offset + offset--------4----9

offset += obj.range.length-1;

}];

//加載到label上

label.attributedText= attrStr;

}

//根據圖片名字獲取圖片資源的具體路徑

- (NSString *)pathForEmoticonWithName:(NSString *)name {

//資源在哪

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Emoticons" ofType:@"bundle"];

//拼接資源路徑

NSString *path1 = [bundlePath stringByAppendingPathComponent:@"com.sina.lxh/info.plist"];

//接收從資源路徑獲取的字典內容

_dic = [NSDictionary dictionaryWithContentsOfFile:path1];

//從字典中提取出圖片包

NSArray *array =_dic[@"emoticons"];

//開始設置要返回的圖片資源路徑

NSString *path =nil;

//遍歷表情包數組里的字典

for(NSDictionary *dicinarray) {

//從字典里找出名字對應的元素

if([dic[@"chs"] isEqualToString:name]) {

//找出圖片名字

NSString *png = [dic objectForKey:@"png"];

//拼接出具體的圖片資源路徑 png = "lxh_xiaohaha.png";

path = [bundlePath stringByAppendingPathComponent:[NSStringstringWithFormat:@"com.sina.lxh/%@",png]];

break;

}

}

//返回具體路徑

return path;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容