Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behavior of NSTextField with NSAttributedString with attachments

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];
like image 495
darvin Avatar asked Sep 17 '25 11:09

darvin


2 Answers

enter image description here

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.

like image 162
billibala Avatar answered Sep 19 '25 06:09

billibala


The above answer by billibala can also be implemented in code. For the NSTextField set

allowsEditingTextAttributes = true
like image 45
JoRa Avatar answered Sep 19 '25 06:09

JoRa