【发布时间】:2017-06-25 09:22:25
【问题描述】:
我正在尝试将图像添加到属性字符串:
NSTextAttachment *locationIcon = [[NSTextAttachment alloc] init];
locationIcon.image = [UIImage imageNamed:@"location-pin-icon"];
NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:locationIcon];
[string appendAttributedString:iconString];
NSAttributedString *locationNameString = [[NSAttributedString alloc] initWithString:@"some text" attributes:linkAttributes];
[string appendAttributedString:locationNameString];
然后我只需设置myLabel.text = string;(其中myLabel 是TTTAttributedLabel)
location-pin-icon 是一个有效的图像(我也在调试中检查过它)。但是,标签中没有显示位置图钉图标(以下“一些文本”显示完美,linkAttributes 只是具有自定义蓝色的系统字体集合)。我也尝试过手动设置文本附件的边界,或者在文本前留一个空格,但似乎没有任何效果。
我做错了什么?
【问题讨论】:
-
如果您打印
string,它会出现吗?另外,您设置attributedString的代码在哪里? -
如果你使用
UILabel(然后是myUILabel.attributedText = string;,它可以工作吗?它还不可用(github.com/TTTAttributedLabel/TTTAttributedLabel/issues/287)问题是很久以前的,但仍然存在。跨度> -
@Larme 当我
po它时它确实出现在我的字符串中,尽管它显示为<NSTextAttachment: 0x1702a7080>对象而不是有用的描述。 -
@Larme 是的,它是 TTTAttributedLabel。我从它切换并开始显示图标。我再也不会相信“直接替换”了,因为这是完全相反的行为:)
标签: ios ios10 nsattributedstring