jer*_*ran 6 cocoa hyperlink swift
我为Mac 10.11在Swift中编写了一个桌面应用程序,我想添加一个链接到About页面。
非常像这样:https : //developer.apple.com/library/mac/qa/qa1487/_index.html
我找不到很好的教程或参考。
任何帮助将非常感激
修改现有答案以允许标签文本的子字符串带有下划线和蓝色,因此您可以执行以下操作:这是答案
// 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
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
最简单的方法是子类化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)!)
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,在 Interface Builder 中,将标签从对象库拖到窗口中。
选择该标签,转到菜单“视图”>“实用程序”>“显示身份检查器”(或按Cmd + Opt + 3)并将类更改为HyperlinkTextField
转到属性检查器( Cmd + Opt + 4) 并设置Href为您要访问的 URL。
标签在 Interface Builder 中显示黑色文本,但当您运行应用程序时一切都会很好。单击标签将在默认浏览器中打开链接。
我无法实现的一件事是HyperlinkTextField在 Interface Builder 中使显示显示为蓝色并带有下划线。欢迎评论如何做到这一点。
| 归档时间: |
|
| 查看次数: |
2913 次 |
| 最近记录: |