我正在为 mac 10.11 快速编写一个桌面应用程序,我想添加一个链接到关于页面。
非常像这样:https://developer.apple.com/library/mac/qa/qa1487/_index.html
我找不到好的教程或引用。
任何帮助将不胜感激
最佳答案
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)
}
}
}
关于swift - Cocoa 和 Swift 中的简单可点击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38340282/