我想在iOS中向UILabel添加正确的小图标
这是我正在使用的代码
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"icon_list"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"Hi"];
[myString appendAttributedString:attachmentString];
cell.categoryTitle.attributedText = myString;
我得到的结果如下,字符串和三个点,任何人都可以告诉我这是什么问题?
答案 0 :(得分:0)
问题是图标大小非常大,我通过调整图标大小来解决它,然后再添加到我的标签
所以工作代码如下:
UIImage *image1 = [UIImage imageNamed:@"icon_ads.png”];
UIImage *myIcon = [self imageWithImage:image1 scaledToSize:CGSizeMake(20, 20)];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = myIcon;
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSAttributedString *titleString = [[NSAttributedString alloc] initWithString:test.catName];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@""];
[myString appendAttributedString:attachmentString];
[myString appendAttributedString:titleString];
cell.categoryTitle.attributedText = myString;