【问题标题】:Strange behavior of NSTextField with NSAttributedString with attachments带有附件的 NSAttributedString 的 NSTextField 的奇怪行为
【发布时间】:2019-08-24 00:27:40
【问题描述】:

当我通过 NSAttributedString 在 NSTextField 中放置图像附件时,当我单击或更改它时,图像会消失。怎么了?

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];

【问题讨论】:

  • 你能在代码中展示你是如何通过 NSAttributedString 做图片附件的吗?
  • 您解决了吗?我对链接也有同样的问题:stackoverflow.com/questions/8684972/…
  • 不是,我没有:(如果你愿意,请告诉我
  • 对此有何见解?我遇到了同样的问题。

标签: objective-c macos cocoa nsattributedstring


【解决方案1】:

实际上,修复非常简单。只需在需要显示图像的字段中启用“富文本”即可。

插入文本附件后,即使将焦点更改为另一个视图,图像也会保留在那里。

【讨论】:

    【解决方案2】:

    上述billibala的回答也可以用代码实现。 对于NSTextField 设置

    allowsEditingTextAttributes = true
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2020-12-08
      • 2015-07-08
      • 1970-01-01
      • 2011-03-16
      相关资源
      最近更新 更多