效果圖
WX20171114-103538.png
NSString *tmpStr = @"測試后面有沒有圖片測試后面有沒有圖片測試后面有沒有圖片[圖片]";
UIFont *font = [UIFont systemFontOfSize:16];
CGFloat imageHeight = font.lineHeight;
// 設置圖片附件
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
UIImage *image = [UIImage imageNamed:@"test.jpg"];
textAttachment.image = image;
// 調(diào)整一下圖片的位置,如果你的圖片偏上或者偏下拣播,調(diào)整一下bounds的y值即可
textAttachment.bounds = CGRectMake(0, -2, imageHeight, imageHeight);
NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *mStr = [[NSMutableAttributedString alloc] initWithString:tmpStr];
NSRange range = [tmpStr rangeOfString:@"[圖片]"];
[mStr replaceCharactersInRange:range withAttributedString:imageStr];
self.label.attributedText = mStr;