-
-
Save jakjothi/ca9657557e9eeb616c5213d70f35f5bb to your computer and use it in GitHub Desktop.
Show link text on NSTextField for OS X by swift.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppDelegate, NSObject, NSApplicationDelegate { | |
@IBOutlet weak var linkField: NSTextField! | |
func applicationDidFinishLaunching(aNotification: NSNotification) { | |
... | |
linkField.attributedStringValue = link_string("Google", url: NSURL(string: "http://www.google.com/")) | |
... | |
} | |
func link_string(text:String, url:NSURL) -> NSMutableAttributedString { | |
// initially set viewable text | |
var attrString = NSMutableAttributedString(string: text) | |
var range = NSRange(location: 0, length: attrString.length) | |
attrString.beginEditing() | |
// stack URL | |
attrString.addAttribute(NSLinkAttributeName, | |
value: url.absoluteString!, range: range) | |
// stack text color | |
attrString.addAttribute(NSForegroundColorAttributeName, | |
value: NSColor.blueColor(), range: range) | |
// stack underline attribute | |
attrString.addAttribute(NSUnderlineStyleAttributeName, | |
value: NSSingleUnderlineStyle, range: range) | |
attrString.endEditing() | |
return attrString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment