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];
效果图:
修改
vipImageAttachment.bounds = CGRectMake(-5, 0, 13, 15);
会把图片前移、但是图片显示不全
效果图:
新增代码
[vipStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, vipStr.length)];
设置文字间距效果图
效果并不理想,只有文字受到NSKernAttributeName
属性影响。图片间距保持原样。
//设置空格文本 [vipStr insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1]; //设置间距 [vipStr addAttribute:NSKernAttributeName value:@(8) range:NSMakeRange(1,1)];
效果图:
最终采用最后一个骚操作、解决问题!
参与评论
手机查看
返回顶部