富文本是为了解决一段文字中添加添加了图片。具体用法如下
NSString *str = @"看着“图片”图标闪烁 ";
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:srt];
UIImage *image = [UIImage imageName:@"图片"];
NSTextAttachment *attach = [[SNTextAttachment alloc] init];
attach.image = image;
CGFloat height = [[self.contentLabel.text sizeWithAttributes:@{NSFontAttributeName:self.contentLabel.font}].height;// 获取label单行文字的高度
self.contentLabel.frame = CGRectMake(CGRectGetMinX(self.contentLabel.frame),CGRectGetMinY(self.contentLael.frame),CGRectGetWidth(self.contentLabel.frame),height*3);//重新设置文本的高度。乘以3是以为有3行文字
NSAttributedString *imageStr = [NSAttributedString attributeStringWithAttachment:attach];
[attriStr insertAttributedString:imageStr atIndex:xx];// 设置图片在文本的位置
self.contentLabel.attributedText = attriStr;