在编辑文本字段时,我尝试将我的自定义触摸条项与触摸栏中的自动文本建议结合起来。

目前,我正在自定义makeTouchBar类中重写NSTextView,如果我不这样做,将为textView创建默认的触摸条。
这是主要的makeTouchBar,在这里,我尝试用项目标识符.candidateList添加建议,但没有结果:
extension ViewController: NSTouchBarDelegate {
override func makeTouchBar() -> NSTouchBar? {
let touchBar = NSTouchBar()
touchBar.delegate = self
touchBar.customizationIdentifier = .myBar
touchBar.defaultItemIdentifiers = [.itemId1,
.flexibleSpace,
.itemId2,
.itemId3,
.flexibleSpace,
.candidateList]
touchBar.customizationAllowedItemIdentifiers = [.itemId1]
return touchBar
}
}有人能提供一个简单的例子,说明如何将这个词、建议项添加到自定义的触摸栏中吗?
发布于 2016-11-09 22:24:12
很简单。只需在您的自定义NSTextView类中调用超级程序:
override func makeTouchBar() -> NSTouchBar {
var touchBar = super.makeTouchBar()
touchBar.delegate = self
var defaultIdentifiers = [Any](arrayLiteral:touchBar.defaultItemIdentifiers)
defaultIdentifiers.insert("CustomLabel", at: 0)
touchBar.defaultItemIdentifiers = defaultIdentifiers
return touchBar
}
override func touchBar(_ touchBar: NSTouchBar, makeItemFor identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem {
if (identifier == "CustomLabel") {
var button = NSButton(title: "Custom", target: self, action: nil)
var item = NSCustomTouchBarItem(identifier: "CustomLabel")
item.view = button
item.customizationLabel = "Custom"
return item
}
else {
return super.touchBar(touchBar, makeItemFor: identifier)
}
return nil
}https://stackoverflow.com/questions/40515787
复制相似问题