【问题标题】:NSTouchBar - how to combine default text filed items with custom itemsNSTouchBar - 如何将默认文本文件项与自定义项结合起来
【发布时间】:2016-11-09 20:49:15
【问题描述】:

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

目前我正在自定义 NSTextView 类中覆盖 makeTouchBar,如果我不这样做,将为 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
    }
}

谁能提供一个简单的例子来说明如何将这个词建议项添加到自定义触摸栏?

【问题讨论】:

    标签: macos cocoa nstextview nstouchbar


    【解决方案1】:

    简单。只需在您的自定义 NSTextView 类中调用 super:

    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
    }
    

    【讨论】:

    • 非常感谢!它适用于从超级获取文本字段触摸栏。但是您的答案中有几行我需要更改:touchBar() 不能返回 nil,而且我不必将 touchBar.defaultItemIdentifiers 强制转换为 [Any]。无论如何,再次感谢您!你帮了我)
    • 对不起我的无知,但是在objective-c中应该如何写return super.touchBar(touchBar, makeItemFor: identifier)
    • return [super touchBar:touchBar makeItemForIdentifier:identifier];
    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多