【问题标题】:NSTableView: blue outline on right-clicked rowsNSTableView:右键单击行上的蓝色轮廓
【发布时间】:2010-11-18 21:28:03
【问题描述】:

我想知道如何摆脱 Cocoa 在 NSTableView / NSOutlineView 中右键单击它们时围绕行绘制的蓝色轮廓。

NSTableView Outline http://tobidobi.com/nstableview_outline.png

如果我没记错的话,它似乎既不是经典的“亮点”也不是“对焦环”——那么,它实际上是什么?

我目前正在完全自己绘制自定义 NSCell - 但我也不知道如何
* 我自己也画这个轮廓,或者
* 摆脱它,或者
* 至少改变它的颜色

非常欢迎任何提示!谢谢!

【问题讨论】:

    标签: cocoa nstableview


    【解决方案1】:

    不幸的是,除了编写自己的表视图替换之外,我不知道有任何记录在案的方法。

    重写的方法是:

    - (void)drawContextMenuHighlightForRow:(NSInteger)row;
    

    请向 Apple 提出改进请求,这样您以后就不必依赖未记录的方法来做您想做的事情。看起来其他两个表格视图突出显示方法在 10.6 中可以自定义,但这个不是。我一直认为它看起来有点笨拙,但有必要指出菜单引用的行(不一定与突出显示的行相同)。

    【讨论】:

    • 谢谢,我已经有一段时间没有做 64 位 Cocoa 了。
    • 这是一个未记录的 API。请告知天气它将通过 Mac App Store 审核流程?
    • 不,通常这不会通过 Mac App Store 审查。 (我在 Mac App Store 存在之前发布了这个答案,所以这不是问题!)
    【解决方案2】:

    我的 NSTableView *mainTableView 没有被子类化,所以我在上下文菜单打开之前就把它去掉了:

    - (void)menuWillOpen:(NSMenu *)menu{
         NSInteger rightClicked = [mainTableView clickedRow];
         [mainTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:rightClicked] byExtendingSelection:NO];
         [mainTableView deselectRow: rightClicked];
         [mainTableView reloadData];
        {
    

    【讨论】:

    • reloadData 是不必要的,但这是要走的路。
    【解决方案3】:

    这是我重新绘制这些轮廓的尝试:

    import Cocoa
    
    class TemplateOutlineView: NSOutlineView {
        
        private static let focusLineWidth: CGFloat = 2.0
        private static let focusLineColor = NSColor.controlAccentColor
        
        @objc
        func drawContextMenuHighlightForRow(_ row: Int) {
            guard !inLiveResize else { return }
            guard let ctx = NSGraphicsContext.current?.cgContext else { return }
            let rects = [CGRect](arrayLiteral: rect(ofRow: row))
            
            ctx.setLineWidth(TemplateOutlineView.focusLineWidth)
            ctx.setStrokeColor(TemplateOutlineView.focusLineColor.cgColor)
            
            let outerRect = bounds
                .insetBy(dx: 0.0, dy: 0.5)
                .offsetBy(dx: 0.0, dy: 0.5)
            for rect in rects.filter({ outerRect.intersects($0) }) {
                let innerRect = rect.intersection(outerRect)
                if innerRect.height > rect.height + 2.0 {
                    ctx.addRect(innerRect
                                    .insetBy(dx: TemplateOutlineView.focusLineWidth, dy: TemplateOutlineView.focusLineWidth + 0.5)
                                    .offsetBy(dx: 0.0, dy: -0.5)
                    )
                } else {
                    ctx.addRect(rect
                                    .insetBy(dx: TemplateOutlineView.focusLineWidth, dy: TemplateOutlineView.focusLineWidth + 0.5)
                                    .offsetBy(dx: 0.0, dy: -0.5)
                    )
                }
            }
            
            ctx.strokePath()
        }
        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2017-07-26
      • 2022-03-09
      • 2022-12-24
      • 1970-01-01
      • 2016-02-16
      • 2020-01-07
      相关资源
      最近更新 更多