我在swift for mac 10.11中編寫桌面應用程序,我想添加一個鏈接到about頁面。Cocoa和Swift中的簡單可點擊鏈接
非常喜歡這樣的:https://developer.apple.com/library/mac/qa/qa1487/_index.html
我一直沒能找到一個很好的教程或參考。
任何幫助,將不勝感激
我在swift for mac 10.11中編寫桌面應用程序,我想添加一個鏈接到about頁面。Cocoa和Swift中的簡單可點擊鏈接
非常喜歡這樣的:https://developer.apple.com/library/mac/qa/qa1487/_index.html
我一直沒能找到一個很好的教程或參考。
任何幫助,將不勝感激
最簡單的方法是繼承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
轉到屬性檢查器(Cmd + Opt + 4
)並將Href
設置爲您要訪問的URL。
標籤顯示在界面生成黑色文本,但是當你運行你的應用程序就萬事大吉了。點擊標籤將打開默認瀏覽器中的鏈接。
我無法實現的一件事是使HyperlinkTextField
在Interface Builder中顯示爲藍色並加下劃線。如何做到這一點的意見是值得歡迎的。
正是我需要的。這裏是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)!)
}
}
修改現有的答案,允許標籤的文本的一個子成爲下劃線和藍色的,所以你可以這樣做:This是答案
// 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
}
}
迅速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.blue,
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)
}
}
}
let link = NSTextField()
link.isBezeled = false
link.drawsBackground = false
link.isEditable = false
link.isSelectable = true
link.allowsEditingTextAttributes = true
let url = URL(string: "http://www.google.com")
let linkTextAttributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue,
NSAttributedStringKey.foregroundColor: NSColor.blue,
NSAttributedStringKey.link: url as Any
]
let string = "whatever"
link.attributedStringValue = NSAttributedString(string: string, attributes: linkTextAttributes)
window.contentView?.addSubview(link)
這很好,非常感謝。 – jeremyforan