美文网首页
iOS 简单实现带图片的Label

iOS 简单实现带图片的Label

作者: 翻滚的炒勺2013 | 来源:发表于2017-07-26 10:40 被阅读61次

先看效果:

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是因为我项目里有几个地方需要传不同的图片,你可以根据自己的需要去修改

相关文章

网友评论

      本文标题:iOS 简单实现带图片的Label

      本文链接:https://www.haomeiwen.com/subject/spynkxtx.html