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

标签 objective-c macos swift nstableview nscell

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

最佳答案

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

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

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 选择更改时检查第一响应者状态。这是必要的,因为表的选择可以更改,即使它不是第一个响应者:

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())
            }
        }

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

//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())
        }
    }

关于objective-c - 在 NSTableView 文本单元格中创建和响应超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244887/

相关文章:

ios - viewForAnnotation 详细披露更改当前位置以及引脚

php - 使用 POST 将文件从 iOS 发送到 PHP

c - 为什么这段 C 代码会在 macOS 上生成段错误,而在其他系统上不会?

objective-c - Swift 中的 CoreData 到数组

swift - 自定义 TableViewCell 没有初始化程序

ios - 如何在 Swift 中将注册用户写入数据库?

ios - 从 Objective C 中的 Json 保存数据的问题

objective-c - Cocoa - 复制邮件按钮栏

sql-server - 如何从 Cocoa (Mac OS X) 访问 SQL Server?

macos - HAL 插件缓冲区大小 kAudioDevicePropertyBufferFrameSize