富文本是為了解決一段文字中添加添加了圖片。具體用法如下
NSString *str = @"看著“圖片”圖標(biāo)閃爍?";
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);//重新設(shè)置文本的高度。乘以3是以為有3行文字
NSAttributedString *imageStr = [NSAttributedString attributeStringWithAttachment:attach];
[attriStr insertAttributedString:imageStr atIndex:xx];// 設(shè)置圖片在文本的位置
self.contentLabel.attributedText = attriStr;