如何将NSButton插入NSTextView? (排队) [英] How to insert a NSButton into a NSTextView? (inline)

查看:113
本文介绍了如何将NSButton插入NSTextView? (排队)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTextView和一个自定义NSButton.我要执行的操作是将该按钮(大小为60x16)插入NSTextView的末尾.
我该怎么做?我一直在尝试如何做到这一点,但是我什么也没找到.
我应该从哪里开始?谢谢

I have a NSTextView and a custom NSButton. What I want to do is to insert that button (60x16 in size) to the end of the 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:

NSTextView/NSTextStorage内部的按钮

问题引用:

我如何使NSButton出现在文本中并对之做出反应 点击次数?

how do I get an NSButton to appear inside the text and react to clicks?

请注意,此问题尚未完全解决,但OP似乎已取得良好的开端.希望您可以从讨论中获得一些线索.

Note that the issue is not fully solved there, but it seems the OP got a good head start. Hopefully you can take some clues from the discussion.

这里是一个答案:

NSTextAttachment保存附件的内容;这就是价值 的NSAttachmentAttributeName属性的 属性字符串中的NSAttachmentCharacter.内容是 通常由NSFileWrapper给出,但这不是必需的;你可以 使用零文件包装器创建一个空的NSTextAttachment.

NSTextAttachment holds the contents of an attachment; it is the value of the NSAttachmentAttributeName attribute for the NSAttachmentCharacter in the attributed string. The contents are usually given by an NSFileWrapper, but this is not required; you can create an empty NSTextAttachment with a nil file wrapper.

NSTextAttachmentCell处理以下内容的显示和用户交互: 依恋.默认情况下,NSTextAttachment将创建一个 NSTextAttachmentCell来显示自身,具体取决于 附件;在一般情况下,这将只是一个图像单元 显示图标.

NSTextAttachmentCell handles display and user interaction for the attachment. By default an NSTextAttachment will create an NSTextAttachmentCell to display itself, depending on the contents of the attachment; in the generic case this will just be an image cell displaying an icon.

但是,如果您愿意,可以提供一个自定义NSTextAttachmentCell用于 您的附件.它不必是该类的成员 NSTextAttachmentCell;它只需要符合 NSTextAttachmentCell协议.实际上,即使那不是严格的 必要的;它只需要实现一些基本方法即可 尺寸和绘图.大多数单元已经这样做了.

If you wish, however, you can supply a custom NSTextAttachmentCell for your attachment. It need not be an member of the class NSTextAttachmentCell; it only needs to conform to the NSTextAttachmentCell protocol. 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.

但是,您将需要自己处理鼠标事件.这 您可能要实现的方法是 wantToTrackMouseForEvent:inRect:ofView:atCharacterIndex:和 trackMouse:inRect:ofView:atCharacterIndex:.此处的字符索引 应该让您确定文本的哪一部分是相关的.

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:. The character index here should let you determine which portion of the text is relevant.

这篇关于如何将NSButton插入NSTextView? (排队)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆