富文本的使用

富文本是为了解决一段文字中添加添加了图片。具体用法如下

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;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容