【问题标题】:Why is NSTextAttachment image not displayed?为什么不显示 NSTextAttachment 图像?
【发布时间】: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;(其中myLabelTTTAttributedLabel

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


【解决方案1】:

这显然是由于TTTAttributedLabel 的实现。图书馆好像坏了。当我从它切换时,附件开始正确显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2015-12-08
    • 2014-07-15
    • 1970-01-01
    • 2022-12-14
    • 2012-07-26
    • 2012-10-26
    相关资源
    最近更新 更多