label 文本中添加图标

2018-11-02  本文已影响0人  一米阳光k1992

在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;

上一篇下一篇

猜你喜欢

热点阅读