Created
December 21, 2016 04:04
-
-
Save a-yasui/068f1b9280f06265afa6fbf0addc65e3 to your computer and use it in GitHub Desktop.
リンク入りテキストを全部オレンジ色にしたいだけのコード。期待通り動かない泣きたい
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 ViewController: NSViewController | |
{ | |
func load(){ | |
let attributeText = NSMutableAttributedString(string: "テスト") | |
let field = NSTextField(frame: NSRect(x: 0, y: 0, width: 30, height: 30)) | |
let attr = [ | |
NSLinkAttributeName: "https://google.com/", | |
] as [String : Any] | |
attributeText.beginEditing() | |
let range = NSRange(location: 0, length: attributeText.length) | |
attributeText.addAttributes(attr, range: range) | |
attributeText.endEditing() | |
let test = NSMutableAttributedString(string:"|") | |
test.beginEditing() | |
test.append(attributeText) | |
test.append(NSAttributedString(string: "|")) | |
let attr1 = [ | |
NSForegroundColorAttributeName: NSColor.orange, | |
NSStrikethroughColorAttributeName: NSColor.orange, | |
NSStrokeColorAttributeName: NSColor.orange, | |
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue, | |
NSFontAttributeName: NSFont(name: "HelveticaNeue-Bold", size: 21.0)!, | |
] as [String : Any] | |
test.addAttributes(attr1, range: NSRange(location:0, length: test.length)) | |
test.endEditing() | |
field.attributedStringValue = test | |
field.drawsBackground = false | |
field.isBordered = false | |
field.isEditable = false | |
field.isSelectable = false | |
field.sizeToFit() | |
self.view.addSubview(field) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment