2015-03-24 59 views
0

我有一个程序,其中有一个NSTableView填充了要上传的文件。一旦文件被发送,带有文件名的文本单元就会得到一个放入其中的超链接(数组数据被赋予一个具有NSLinkAttributeName属性的NSMutableString)。我如何让用户点击此链接在默认浏览器中打开网页?创建并响应NSTableView文本单元格内的超链接

回答

2

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

创建扩展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的选择发生变化时。这是必要的,因为表的选择是可以改变的,即使它不是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)) 
    } 
} 

添加志愿检查时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()) 
     } 
    } 
0

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

+0

抱歉,NSTableView的的细胞是文本单元,而不是文本视图。我已经将超文本链接设置为“可选”,但我仍然无法点击链接打开它。我必须使用不同于Text Cell的单元吗?谢谢! – 2015-03-31 23:01:21

+0

文本单元格是否配置为允许富文本?这对于使用属性文本也是必要的。 – 2015-03-31 23:34:28

+0

是的,单元格被配置为允许富文本,是可选的,并且该动作设置为“仅在发送时输入”。现在我一直在使用NSTable的tableViewSelectionDidChange,但这感觉更像是一种黑客,而不是实际上给予我想要的行为。 – 2015-04-07 21:25:51