简体   繁体   English

如何将NSButton插入NSTextView? (排队)

[英]How to insert a NSButton into a NSTextView? (inline)

I have a NSTextView and a custom NSButton. 我有一个NSTextView和自定义的NSButton。 What I want to do is to insert that button (60x16 in size) to the end of the NSTextView. 我想要做的是将该按钮(大小为60x16)插入NSTextView的末尾。
How can I do something like that? 我该怎么做? I've been trying to search around on how to do this but I'm not getting anywhere. 我一直在寻找如何做到这一点,但我什么也没找到。
Where should I begin? 我应该从哪里开始? Thanks 谢谢

I believe this question is pretty similar to yours: 我相信这个问题与您的问题非常相似:

Buttons inside an NSTextView / NSTextStorage NSTextView / NSTextStorage内部的按钮

Quote from the question: 引用问题:

how do I get an NSButton to appear inside the text and react to clicks? 如何使NSButton出现在文本中并对点击做出反应?

Note that the issue is not fully solved there, but it seems the OP got a good head start. 请注意,此问题尚未完全解决,但OP似乎已取得良好的开端。 Hopefully you can take some clues from the discussion. 希望您可以从讨论中获得一些线索。

Here is one answer: 这是一个答案:

NSTextAttachment holds the contents of an attachment; NSTextAttachment保存附件的内容; it is the value of the NSAttachmentAttributeName attribute for the NSAttachmentCharacter in the attributed string. 它是属性字符串中NSAttachmentCharacter的NSAttachmentAttributeName属性的值。 The contents are usually given by an NSFileWrapper, but this is not required; 内容通常由NSFileWrapper给出,但这不是必需的。 you can create an empty NSTextAttachment with a nil file wrapper. 您可以使用nil文件包装器创建一个空的NSTextAttachment。

NSTextAttachmentCell handles display and user interaction for the attachment. NSTextAttachmentCell处理附件的显示和用户交互。 By default an NSTextAttachment will create an NSTextAttachmentCell to display itself, depending on the contents of the attachment; 默认情况下,NSTextAttachment将创建一个NSTextAttachmentCell来显示自身,具体取决于附件的内容。 in the generic case this will just be an image cell displaying an icon. 在一般情况下,这只是一个显示图标的图像单元。

If you wish, however, you can supply a custom NSTextAttachmentCell for your attachment. 但是,如果您愿意,可以为附件提供一个自定义的NSTextAttachmentCell。 It need not be an member of the class NSTextAttachmentCell; 它不必是NSTextAttachmentCell类的成员。 it only needs to conform to the NSTextAttachmentCell protocol. 它只需要符合NSTextAttachmentCell协议即可。 Actually, even that is not strictly necessary; 实际上,即使那不是严格必要的; it only needs to implement a few of the basic methods for sizing and drawing. 它只需要实现一些基本的尺寸和绘图方法。 Most cells already do this. 大多数细胞已经这样做了。

You will, however, need to deal with mouse events yourself. 但是,您将需要自己处理鼠标事件。 The methods you'll probably want to implement would be wantsToTrackMouseForEvent:inRect:ofView:atCharacterIndex: and trackMouse:inRect:ofView:atCharacterIndex:. 您可能要实现的方法是wantToTrackMouseForEvent:inRect:ofView:atCharacterIndex:和trackMouse:inRect:ofView:atCharacterIndex:。 The character index here should let you determine which portion of the text is relevant. 此处的字符索引应让您确定文本的哪一部分是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM