在 NSTableView 文本单元格中创建和响应超链接

Create and Respond to a Hyperlink within a NSTableView Text Cell

我有一个程序,其中包含一个 NSTableView,其中填充了要上传的文件。发送文件后,带有文件名的文本单元格会得到一个 hyperlink 放入其中(数组数据被赋予一个带有 NSLinkAttributeName 属性的 NSMutableString)。如何允许用户单击此 link 以在其默认浏览器中打开网页?

文本字段自动支持点击嵌入式链接,但前提是它们至少可以选择(如果不可编辑)。因此,将您的文本字段设置为可选。

经过多次搜索和尝试多种方法,这就是我想出的解决方案。

创建一个扩展 NSTableViewCell 的自定义 class:

class TableViewCellCursor: NSTableCellView {

internal var active = false

//MARK: - View Life Cycle

override func awakeFromNib() {
    superview?.awakeFromNib()
    self.createTrackingArea()
}

//MARK: - IBActions

override func mouseEntered(theEvent: NSEvent) {
    if (NSCursor.currentCursor() == NSCursor.arrowCursor() && active) {
        NSCursor.pointingHandCursor().set()
    }
}

override func mouseExited(theEvent: NSEvent) {
    if (NSCursor.currentCursor() == NSCursor.pointingHandCursor() && active) {
        NSCursor.arrowCursor().set()
    }
}

//Informs the receiver that the mouse cursor has moved into a cursor rectangle.
override func cursorUpdate(event: NSEvent) {
    if (active) {
        NSCursor.pointingHandCursor().set()
    }
}

//MARK: - Util

func createTrackingArea() {
    var focusTrackingAreaOptions:NSTrackingAreaOptions = NSTrackingAreaOptions.ActiveInActiveApp
    focusTrackingAreaOptions |= NSTrackingAreaOptions.MouseEnteredAndExited
    focusTrackingAreaOptions |= NSTrackingAreaOptions.AssumeInside
    focusTrackingAreaOptions |= NSTrackingAreaOptions.InVisibleRect

    var focusTrackingArea:NSTrackingArea = NSTrackingArea(rect: NSZeroRect,
                                                        options: focusTrackingAreaOptions,
                                                        owner: self, userInfo: nil)
    self.addTrackingArea(focusTrackingArea)
}
}

在 NSTableView 选择更改时检查第一响应者状态。这是必要的,因为可以更改 table 的选择,即使它不是 firstResponder:

func tableViewSelectionDidChange(aNotification: NSNotification) {
    if (self.firstResponder == filesToTransferTable) {
        changeSelectedRowTextColorTo(NSColor.whiteColor(), unselectedColor: NSColor.blueColor())
    } else {
        changeSelectedRowTextColorTo(NSColor.blackColor(), unselectedColor: NSColor.blueColor())
    }
}

func changeSelectedRowTextColorTo(selectedColor: NSColor, unselectedColor: NSColor) {
    let selectedRows = filesToTransferTable.selectedRowIndexes
    for (index, tableEntry) in enumerate (tableData) {
        if tableData[index]["FileName"] is NSMutableAttributedString {
            var name = tableData[index]["FileName"] as! NSMutableAttributedString
            var range = NSMakeRange(0, NSString(string:name.string).length)
            name.beginEditing()
            name.removeAttribute(NSForegroundColorAttributeName, range: range)

            if (selectedRows.containsIndex(index)) {
                name.addAttribute(NSForegroundColorAttributeName, value:selectedColor, range:range)
            } else {
                name.addAttribute(NSForegroundColorAttributeName, value:unselectedColor, range:range)
            }

            name.endEditing()
            tableData[index]["FileName"] = name
        }
        filesToTransferTable.reloadDataForRowIndexes(NSIndexSet(index: index), columnIndexes: NSIndexSet(index:0))
    }
}

添加 KVO 以检查 FirstResponder 何时更改:

//This is somewhere in your code where you initialize things
//KVO for first responder behavior regarding tableView and updating attributedStrings' colors
self.addObserver(self, forKeyPath: "firstResponder", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: nil)

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
            if (change[NSKeyValueChangeNewKey] is NSTableView) {
                changeSelectedRowTextColorTo(NSColor.whiteColor(), unselectedColor: NSColor.blueColor())
            } else if (change[NSKeyValueChangeOldKey] is NSTableView) {
                changeSelectedRowTextColorTo(NSColor.blackColor(), unselectedColor: NSColor.blueColor())
            }
        }

最后,检查主要 window(应用程序本身)是否处于焦点(如果没有这样做,那么当 window 失去焦点时颜色将不会适当改变):

//Put these in the same place as the KVO code
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "windowDidBecomeKey:",
                    name: NSWindowDidBecomeKeyNotification , object: self)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "windowDidResignKey:",
                    name: NSWindowDidResignKeyNotification , object: self)


    func windowDidBecomeKey(notification: NSNotification) {
        if (self.firstResponder == filesToTransferTable) {
            changeSelectedRowTextColorTo(NSColor.whiteColor(), unselectedColor: NSColor.blueColor())
        } else {
            changeSelectedRowTextColorTo(NSColor.blackColor(), unselectedColor: NSColor.blueColor())
        }
    }

    func windowDidResignKey(notification: NSNotification) {
        if (self.firstResponder == filesToTransferTable) {
            changeSelectedRowTextColorTo(NSColor.blackColor(), unselectedColor: NSColor.blueColor())
        }
    }