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;

你可能感兴趣的:(label 文本中添加图标)