Created
December 28, 2014 17:16
-
-
Save chsh/ecf534ca79a8cf11e49d to your computer and use it in GitHub Desktop.
Show link text on NSTextField for OS X by swift.
This file contains 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
linkField should be specified "Rich Text" flag on IB or defined on code.