我有一个程序,其中有一个NSTableView填充了要上传的文件。一旦文件被发送,带有文件名的文本单元就会得到一个放入其中的超链接(数组数据被赋予一个具有NSLinkAttributeName属性的NSMutableString)。我如何让用户点击此链接在默认浏览器中打开网页?创建并响应NSTableView文本单元格内的超链接
0
A
回答
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
文本字段自动支持单击嵌入式链接,但前提是它们至少可以选择(如果不可编辑)。所以,设置你的文本字段是可选的。
相关问题
- 1. 如何在单元格文本中创建超链接?
- 2. 使用单元格内容创建超链接的VBA代码
- 3. 在Excel单元格内创建超链接?
- 4. Excel VBA:从超链接内的单元格插入文本
- 5. 在XSL中搜索文本元素并创建超链接
- 6. 在另一个单元格在Excel中创建的文本超链接
- 7. excel链接单元格并保留主单元格中的超链接
- 8. 超链接到单元格?
- 9. 超链接与单元格链接
- 10. 如何创建包含图像超链接的JTable单元格?
- 11. 使活动单元格超链接到新创建的标签
- 12. 在Winform DataGridView中创建响应超链接链接
- 13. 跨表格单元格的超链接?
- 14. VBA查找匹配的单元格并为它们创建超链接
- 15. 基于单元格内容的文件超链接
- 16. Excel VBA:如何从单元格值自动创建超链接?
- 17. 从纯文本创建超链接
- 18. 从文本创建超链接
- 19. 如何创建超链接在本文
- 20. 带有超链接文本的单元格引用
- 21. Excel单元格有超链接格式
- 22. Google表格超链接响应代码
- 23. Infragistics UltraGrid中的超链接单元格
- 24. delphi excel单元格中的超链接
- 25. GWT中的超链接单元格CellTable
- 26. Excel 2010 - 超链接文本而不是整个单元格
- 27. Excel超链接单元格到文本框
- 28. 如何在单元格文本中制作超链接?
- 29. 添加链接到单元格内的文本,而不是整个单元格
- 30. 基于另一个单元格值的超链接单元格
抱歉,NSTableView的的细胞是文本单元,而不是文本视图。我已经将超文本链接设置为“可选”,但我仍然无法点击链接打开它。我必须使用不同于Text Cell的单元吗?谢谢! – 2015-03-31 23:01:21
文本单元格是否配置为允许富文本?这对于使用属性文本也是必要的。 – 2015-03-31 23:34:28
是的,单元格被配置为允许富文本,是可选的,并且该动作设置为“仅在发送时输入”。现在我一直在使用NSTable的tableViewSelectionDidChange,但这感觉更像是一种黑客,而不是实际上给予我想要的行为。 – 2015-04-07 21:25:51