Cocoa 和 Swift 中的简单可点击链接 [英] Simple clickable link in Cocoa and Swift
本文介绍了Cocoa 和 Swift 中的简单可点击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 swift 中为 mac 10.11 编写一个桌面应用程序,我想添加一个指向关于页面的链接.
Im writing a desktop app in swift for mac 10.11 and I would like to add a link to the about page.
非常像这样:https://developer.apple.com/library/mac/qa/qa1487/_index.html
我一直找不到好的教程或参考资料.
I haven't been able to find a good tutorial or reference.
任何帮助将不胜感激
推荐答案
Swift 4, xCode 9
Swift 4, xCode 9
@IBDesignable
class HyperlinkTextField: NSTextField {
@IBInspectable var href: String = ""
override func resetCursorRects() {
discardCursorRects()
addCursorRect(self.bounds, cursor: NSCursor.pointingHand)
}
override func awakeFromNib() {
super.awakeFromNib()
// TODO: Fix this and get the hover click to work.
let attributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.foregroundColor: NSColor.linkColor,
NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue as AnyObject
]
attributedStringValue = NSAttributedString(string: self.stringValue, attributes: attributes)
}
override func mouseDown(with theEvent: NSEvent) {
if let localHref = URL(string: href) {
NSWorkspace.shared.open(localHref)
}
}
}
这篇关于Cocoa 和 Swift 中的简单可点击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文