【问题标题】:How to change the selection color of a sidebar?如何更改侧边栏的选择颜色?
【发布时间】:2020-01-10 07:15:18
【问题描述】:

有没有办法改变侧边栏项目的选择颜色? 例如 macOS 10.14.x 深色主题的默认颜色是蓝色,是否可以更改此颜色? 我看了一下这里:Cocoa osx NSTableview change row highlight color 但我很难翻译成 Applescript,在此先感谢。

【问题讨论】:

  • 您的表格视图单元格还是基于视图的,到目前为止您做了什么?你到底在翻译什么问题?
  • 嘿@red_menace,它基于表格视图单元格,老实说,我不知道 Objective-c 代码的哪一部分适合我的脚本,它适合哪里,以及它应该如何进行。
  • @red_menace,你能给我指个路吗?
  • 我不得不挖出一个旧的表格视图示例应用程序来测试一下,以免让自己太尴尬,但我的回答应该能让你开始。
  • 非常感谢,我会尝试的,但为了让您知道我完全按照@Ted Wrigley 在本主题中的回答:stackoverflow.com/questions/57384941/…

标签: applescript-objc


【解决方案1】:

如果没有示例或 MCVE 项目,提供即插即用的解决方案将会很棘手。您链接到的主题和答案覆盖了 NSTableViewNSTableRowView 类,因此我可以为您提供一个通用的解决方案。 AppleScriptObjC 中的子类化可能有点麻烦,具体取决于您需要引用的内容,但相当简单。本质上,您是在常规类和应用程序之间放置一个自定义类,您可以在其中拦截各种标准方法调用。

对于基于单元格的表格视图示例,使用文件 > 新建 > 文件... 菜单项将新的空文件添加到您的项目中。 _file 的名称并不重要(例如TableViewHighlight.applescript 或其他),script 的名称将被 Xcode 使用。在这里,我使用 MyTableView 作为类名并从 AppDelegate 引用 tableView 插座属性:

script MyTableView -- the name of your custom class

    property parent : class "NSTableView" -- the parent class to override
    property tableView : a reference to current application's NSApp's delegate's tableView
    property highlightColor : a reference to current application's NSColor's greenColor -- whatever

    # set the row highlight color
    on drawRow:row clipRect:clipRect
        if tableView's selectedRowIndexes's containsIndex:row then -- filter as desired
            highlightColor's setFill()
            current application's NSRectFill(tableView's rectOfRow:row)
        end if
        continue drawRow:row clipRect:clipRect -- super
    end drawRow:clipRect:

    # set the highlight color of stuff behind the row (grid lines, etc)
    on drawBackgroundInClipRect:clipRect
        highlightColor's setFill()
        current application's NSRectFill(clipRect)
        continue drawBackgroundInClipRect:clipRect -- super
    end drawBackgroundInClipRect:

end script

界面编辑器中,使用Identity Inspector将表格视图的类设置为MyTableView类。最后,在您的表格视图设置中将其突出显示设置为无,因为它将由您的子类完成(再次假设tableView 出口连接到表格视图):

    tableView's setSelectionHighlightStyle: current application's NSTableViewSelectionHighlightStyleNone

对于基于视图的表格视图示例,过程类似,但NSTableRowView是子类化的。这里我使用的脚本/类的名称是MyTableRowView

script MyTableRowView -- the name of your custom class

    property parent : class "NSTableRowView" -- the parent class to override
    property highlightColor : a reference to current application's NSColor's redColor -- whatever

    # draw the selected row
    on drawSelectionInRect:dirtyRect
        continue drawSelectionInRect:dirtyRect
        highlightColor's setFill()
        current application's NSRectFill(dirtyRect)
    end drawSelectionInRect:

end script

界面编辑器中,使用Attributes Inspector将表格视图的高亮设置为常规,并向表格视图的委托添加tableView:rowViewForRow:方法:

on tableView:tableView rowViewForRow:row
    set rowIdentifier to "MyTableRow"
    set myRowView to tableView's makeViewWithIdentifier:rowIdentifier owner:me
    if myRowView is missing value then
        set myRowView to current application's MyTableRowView's alloc's initWithFrame:current application's NSZeroRect
        myRowView's setIdentifier:rowIdentifier
    end if
    return myRowView
end tableView:rowViewForRow:

当然还有其他选择,但这应该可以帮助您入门,并帮助翻译其中一些 Objective-C 答案/示例。

【讨论】:

  • imgur.com/a/Qsfj6F3,ops,我好像没看明白,我把项目的链接留下:sendspace.com/file/8z3tim
  • 我从您对我最初评论的回复中了解到,您的表格视图是基于单元格的,但在您的示例中它是基于视图的,因此我必须进行一些更改。
  • 我很抱歉这个错误。
  • 没问题,但这就是为什么在这里构建应用程序有点离题 - 它往往变得过于复杂并且难以封装到单个主题中,特别是如果您使用界面编辑器很多绑定。如果您能提供一个简单的样本来证明您的具体问题,那么您的运气通常会更好。
  • 有没有可能使用表格视图的高亮作为源列表?
猜你喜欢
  • 2018-01-30
  • 1970-01-01
  • 2019-07-29
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 2020-12-20
相关资源
最近更新 更多