When I place image attachment in NSTextField via NSAttributedString, when I click it or change it, images disappears. What's wrong?
NSTextAttachment* attachment = [[NSTextAttachment alloc] init];
NSImage *symbolImage = [NSImage imageNamed:@"enabled.png"];
NSLog(@"%@", symbolImage);
NSTextAttachmentCell *anAttachmentCell = [[NSTextAttachmentCell
alloc] initImageCell:symbolImage];
[attachment setAttachmentCell:anAttachmentCell];
return [NSAttributedString attributedStringWithAttachment:attachment];
Actually, the fix is quite straight forward. Just enable "Rich Text" to the field where you need to display image.
Once text attachment is inserted, image remains there even after changing focus to another view.
The above answer by billibala can also be implemented in code.
For the NSTextField
set
allowsEditingTextAttributes = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With