UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
lbl.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:lbl];
// 0、創(chuàng)建一個空的可變屬性字符串
NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] init];
// 1、創(chuàng)建一個屬性字符串
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"d_aini"];
// 調(diào)整圖片位置
attachment.bounds = CGRectMake(0, -4, lbl.font.lineHeight, lbl.font.lineHeight);
// 2、根據(jù)附件創(chuàng)建屬性字符串
NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attachment];
// 3、創(chuàng)建文字屬性字符串
NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:@"小碼哥"];
// 4、創(chuàng)建文件屬性字符串
NSAttributedString *str2 = [[NSAttributedString alloc] initWithString:@"愛你"];
// 5、拼接變屬性字符串
[strM appendAttributedString:str1];
[strM appendAttributedString:str];
[strM appendAttributedString:str2];
// 6、給lbl賦值,圖文混排
lbl.attributedText = strM;
效果如下: