我有一个通知中心窗口小部件,其中包含一个带有表视图单元格的表视图。在单元格中,我有一个标签,我想用文字+图像显示。该图像作为NSTextAttachment包含在内。我在应用程序中有以下代码:
NSTextAttachment *attachment = [NSTextAttachment new];
attachment.image = [UIImage imageWithData:item.image];
NSAttributedString *itemImage = [NSAttributedString attributedStringWithAttachment:attachment];
NSAttributedString *itemName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@", item.name]];
NSMutableAttributedString *itemString = [[NSMutableAttributedString alloc] initWithAttributedString:itemImage];
[itemString appendAttributedString:itemName];
cell.nameLabel.attributedText = itemString;
此代码在应用程序内部工作,但我也尝试在我的小部件(因此TodayViewController)中使用。在窗口小部件中显示时,标签上不会显示任何图像。如果我在运行此代码时停止,我可以看到attachment.image正确设置。我究竟做错了什么?谢谢!
答案 0 :(得分:1)
后来,我设置了cell.nameLabel.attributedText = itemString;
后我设置了cell.nameLabel.text = itemString,所以它覆盖了属性字符串
答案 1 :(得分:0)
尝试更改
NSTextAttachment *attachment = [NSTextAttachment new];
到
NSTextAttachment *attachment = [NSTextAttachment alloc] initWithData:nil ofType:nil];
这篇帖子可能是同一个问题:NSTextAttachment image not displayed on iOS 8 devices (with iOS7.1 sdk)