美文网首页
Label 富文本设置图片

Label 富文本设置图片

作者: 爱喝农药de清凉 | 来源:发表于2017-07-20 15:15 被阅读28次

文字中添加图片

步骤如下:

创建NSTextAttachment的对象,用来装在图片
将NSTextAttachment对象的image属性设置为想要使用的图片
设置NSTextAttachment对象bounds大小,也就是要显示的图片的大小
用[NSAttributedString attributedStringWithAttachment:attch]方法,将图片添加到富文本上

// 添加表情
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情图片
attch.image = [UIImage imageNamed:@"d_aini"];
// 设置图片大小
attch.bounds = CGRectMake(0, 0, 32, 32);

// 创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attri appendAttributedString:string];

// 用label的attributedText属性来使用富文本
self.textLabel.attributedText = attri;

相关文章

网友评论

      本文标题:Label 富文本设置图片

      本文链接:https://www.haomeiwen.com/subject/nkzdkxtx.html