label 文本中添加图标

在Label 文本中添加图标

NSMutableAttributedString *textAttrStr = [[NSMutableAttributedString alloc] initWithString:self.address];

        NSTextAttachment *attach = [[NSTextAttachment alloc] init];

        attach.image= [UIImageimageNamed:@"address_copy"];

        attach.bounds=CGRectMake(0, -3,16,16);//设置图片位置 (不太好用)设置间距无效果

        NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];

        [textAttrStrappendAttributedString:imgStr];

        if(self.address.length>0) {

//设置图片与文字间距

            [textAttrStraddAttribute:NSKernAttributeName value:@(4) range:NSMakeRange(self.address.length - 1, 1)];

        }

        self.customAddressLb.attributedText= textAttrStr;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。