您的位置:首页 > 产品设计 > UI/UE

iOS UILabel内容嵌入本地小图标

2018-04-02 16:14 169 查看
直接上图



实现代码如下:

//设置Vip标志
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
//设置目标图片
imageAttachment.image = [UIImage imageNamed:imageName];
//设置大小和位置
imageAttachment.bounds = CGRectMake(-5, -3, imageAttachment.image.size.width, imageAttachment.image.size.height);
//初始化富文本字符串 并携带NSTextAttachment
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
//初始化一个空的可变富文本字符串
NSMutableAttributedString *completeText= [[NSMutableAttributedString alloc] initWithString:@""];
//拼接之前的attachmentString字符串
[completeText appendAttributedString:attachmentString];
//初始化目标富文本字符串
NSMutableAttributedString *textAfterIcon= [[NSMutableAttributedString alloc] initWithString:title];
//拼接目标富文本内容
[completeText appendAttributedString:textAfterIcon];
/*备注:上边这样的顺序执行下来是先小图标在文本内容如上图一样,如果你图标想放在后面,就无需多拼接一次,直接用目标文本去拼接图标就可以,[completeText appendAttributedString:attachmentString];*/
[_myLabel setAttributedText:completeText];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息