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

58 0

我在swift for mac 10.11中编写了一个桌面应用程序,我想添加一个指向about页面的链接。

非常类似于:https://developer.apple.com/library/mac/qa/qa1487/_index.html

我找不到好的教程或参考资料。

任何帮助都会非常感激

时间:

125 5

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)


 }


 }


}



71 2

最简单的方法是子类化 NSTextField 来创建 HyperlinkTextField 。 下面是一个示例:

首先,让我们向你的项目添加一个 HyperlinkTextField 类:


//HyperlinkTextField.swift



import Cocoa



@IBDesignable


class HyperlinkTextField: NSTextField {


 @IBInspectable var href: String =""



 override func awakeFromNib() {


 super.awakeFromNib()



 let attributes: [String: AnyObject] = [


 NSForegroundColorAttributeName: NSColor.blueColor(),


 NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue


 ]


 self.attributedStringValue = NSAttributedString(string: self.stringValue, attributes: attributes)


 }



 override func mouseDown(theEvent: NSEvent) {


 NSWorkspace.sharedWorkspace().openURL(NSURL(string: self.href)!)


 }


}



接下来,在 Interface Builder中,将一个标签从对象库拖到你的窗口。

选择标签,转到菜单视图> 实用程序> 显示身份检查器( 或按 Cmd + Opt + 3 ) 将类更改为 HyperlinkTextField

Identity Inspector

转到属性检查器( Cmd + Opt + 4 ) 并将 Href 设置为要访问的URL 。

Attributes Inspector

标签在接口生成器中显示黑色文本,但在运行应用程序时一切都会正常。 单击标签将打开默认浏览器中的链接。

我无法实现的一件事是让 HyperlinkTextField 在接口构建器中显示为蓝色且带下划线。

139 3

正是我需要的。这是Swift3版本:


import Cocoa



@IBDesignable


class HyperTextField: NSTextField {


 @IBInspectable var href: String =""



 override func awakeFromNib() {


 super.awakeFromNib()



 let attributes: [String: AnyObject] = [


 NSForegroundColorAttributeName: NSColor.blue,


 NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue as AnyObject


 ]


 self.attributedStringValue = NSAttributedString(string: self.stringValue, attributes: attributes)


 }



 override func mouseDown(with event: NSEvent) {


 NSWorkspace.shared().open(URL(string: self.href)!)


 }


}



65 5

修改了现有的答案以允许标签文本的子字符串变为下划线和蓝色,因此你可以执行以下操作: 这个就是答案


//A text field that can contain a hyperlink within a range of characters in the text.


@IBDesignable


public class SubstringLinkedTextField: NSTextField {


//the URL that will be opened when the link is clicked.


 public var link: String =""


 @available(*, unavailable, message:"This property is reserved for Interface Builder. Use 'link' instead.")


 @IBInspectable public var HREF: String {


 get {


 return self.link


 }


 set {


 self.link = newValue


 self.needsDisplay = true


 }


 }



//the substring within the field's text that will become an underlined link. if empty or no match found, the entire text will become the link.


 public var linkText: String =""


 @available(*, unavailable, message:"This property is reserved for Interface Builder. Use 'linkText' instead.")


 @IBInspectable public var LinkText: String {


 get {


 return self.linkText


 }


 set {


 self.linkText = newValue


 self.needsDisplay = true


 }


 }



 override public func awakeFromNib() {


 super.awakeFromNib()



 self.allowsEditingTextAttributes = true


 self.isSelectable = true



 let url = URL(string: self.link)


 let attributes: [NSAttributedStringKey: AnyObject] = [


 NSAttributedStringKey(rawValue: NSAttributedStringKey.link.rawValue): url as AnyObject


 ]


 let attributedStr = NSMutableAttributedString(string: self.stringValue)



 if self.linkText.count> 0 {


 if let range = self.stringValue.indexOf(substring: self.linkText) {


 attributedStr.setAttributes(attributes, range: range)


 } else {


 attributedStr.setAttributes(attributes, range: NSMakeRange(0, self.stringValue.count))


 }


 } else {


 attributedStr.setAttributes(attributes, range: NSMakeRange(0, self.stringValue.count))


 }


 self.attributedStringValue = attributedStr


 }


}



...