在开始写之前先看一下效果图
在此效果图中有富文本中指定的位置添加图片,还有最后位置添加的图片信息
代码如下
调用方法-------
给label赋值
NSString * Str = @"中国人民解放军万岁,中华人民共和国万岁,万岁!";
self.attrobiuteLabel.attributedText = [self stringWithUIImage:Str];
1:创建富文本,设置样式
// 添加单个属性
//- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
// 创建一个富文本
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
// 修改富文本中的不同文字的样式
[attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];
[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];
2:将图片添加到指定的富文本的位置中
/**
添加图片到指定的位置
*/
NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
// 表情图片
attchImage.image = [UIImage imageNamed:@"jiedu"];
// 设置图片大小
attchImage.bounds = CGRectMake(0, 0, 40, 15);
NSAttributedString *s