先看效果:

01B3D368-9C12-46F7-B0B7-F41705B67CF4.png
- (NSAttributedString *)stringWithImageName:(NSString *) imageName content: (NSString *)content {
/// 创建一个富文本
NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:content];
// 修改富文本中的不同文字的样式
[attriStr addAttribute:NSForegroundColorAttributeName value:ZCDarkGrayColor range:NSMakeRange(0,content.length)];
[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:8] range:NSMakeRange(0,content.length)];
/// 创建图片附件
NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
attchImage.image = [UIImage imageNamed:imageName];
attchImage.bounds = CGRectMake(-1, -1, 9, 9);
// 设置图片大小
NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
/// 插入图片的位置
[attriStr insertAttributedString:stringImage atIndex:0];
return attriStr;
}
代码很简单就不多说了, imageName是你要传图片的名字,type是因为我项目里有几个地方需要传不同的图片,你可以根据自己的需要去修改
网友评论