1.NSMutableAttributedString如何显示图片
NSMutableAttributedString *vipStr = [[NSMutableAttributedString alloc]initWithString:
@"点击解锁【超级方法】"];
UIImage *vipImage = [UIImage imageNamed:
@"WordDetailNor解锁"];
NSTextAttachment *vipImageAttachment =
[[NSTextAttachment alloc]init];
vipImageAttachment.image =
vipImage;
vipImageAttachment.bounds = CGRectMake(
0,
0,
13,
15);
NSAttributedString *vipImageAttrStr =
[NSAttributedString attributedStringWithAttachment:vipImageAttachment];
[vipStr insertAttributedString:vipImageAttrStr atIndex:0];
//设置字体和设置字体的范围
[vipStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0f] range:NSMakeRange(
0, vipStr.length)];
[self.noteLabel setAttributedText:vipStr];
效果图:
2、图片和字体紧密相连、如何让图片和文字间有间隙呢?
修改
vipImageAttachment.bounds = CGRectMake(
-5,
0,
13,
15);
会把图片前移、但是图片显示不全
效果图:
3、设置图片间以及文字间距
新增代码
[vipStr addAttribute:NSKernAttributeName value:@(
10) range:NSMakeRange(
0, vipStr.length)];
设置文字间距效果图
效果并不理想,只有文字受到NSKernAttributeName属性影响。图片间距保持原样。
4、在每张图片后面添加空格,设置空格间文字间距、解决问题
//设置空格文本
[vipStr insertAttributedString:[[NSAttributedString alloc] initWithString:
@" "] atIndex:
1];
//设置间距
[vipStr addAttribute:NSKernAttributeName value:@(
8)
range:NSMakeRange(1,
1)];
效果图:
最终采用最后一个骚操作、解决问题!