Search code examples
iosobjective-cios8ios8-today-widget

NSTextAttachment not showing up in Today Notification Center Widget


I have a notification center widget that has a table view with table view cells. In the cells I have a label that I would like to show up with text + an image. The image being included as an NSTextAttachment. I have the following code inside the app:

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;

This code works while inside the app but I am also trying to use in my widget (and therefore TodayViewController). When displayed in the widget, no image shows up on the label. If I stop while running through this code I can see that the attachment.image is getting set properly. What am I doing wrong? Thanks!


Solution

  • It turned out later on, after I had set cell.nameLabel.attributedText = itemString; I was later setting cell.nameLabel.text = itemString, so it was overriding the attributed string