我想实现一个NSTokenField,它将显示令牌 - 当鼠标悬停在令牌上时 - 显示一个删除图标.随后当我点击图标时,我希望删除令牌.
经过大量搜索后,似乎标准NSTokenField无法实现这一点.如果有人知道如何请让我知道.
我查看了https://github.com/octiplex/OEXTokenField,并根据该代码在Swift中创建了一个CustomTokenField实现.到目前为止,我有一个工作的CustomTokenField,当我将鼠标悬停在令牌上时,它会显示一个删除图标.
下一阶段结果是一个我无法弄明白的问题.如何点击令牌触发回调.?
令牌类派生自NSTextAttachmentCell,CustomTokenField派生自NStokenField:
class CustomTokenAttachmentCell: NSTextAttachmentCell {
. . .
}
class CustomTokenField: NSTokenField {
. . .
}
Run Code Online (Sandbox Code Playgroud)
我试图用两个不同的角度来解决这个问题:
NSTextAttachmentCell实现NSTextAttachmentCellProtocol.
public protocol NSTextAttachmentCellProtocol : NSObjectProtocol {
. . .
public func wantsToTrackMouse() -> Bool
public func highlight(flag: Bool, withFrame cellFrame: NSRect, inView controlView: NSView?)
public func trackMouse(theEvent: NSEvent, inRect cellFrame: NSRect, ofView controlView: NSView?, untilMouseUp flag: Bool) -> Bool
. …
Run Code Online (Sandbox Code Playgroud)