swift - Cocoa 和 Swift 中的简单可点击链接

标签 swift cocoa hyperlink

我正在为 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/

相关文章:

ios - 是否有 API 可以告诉谁取消了他们的应用内购买?

objective-c - 如何在一个 bundle 中创建几个应用程序?

css - 如何在图像代码中放置超链接?

ios - 如何从最新到最旧的顺序获取图像到 Collection View

ios - 以编程方式定义新的导航 Controller 顺序/堆栈?

swift - 普通类型的变量不能与 nil 进行比较,但文字可以,这是为什么呢?

objective-c - cocoa NSTextField 行间距

cocoa - NSString 对象比较 nil

url - 有没有一种方法可以禁止电子邮件引擎自动超链接URL?

WPF - 从数据获取超链接命令?