Skip to content

Instantly share code, notes, and snippets.

@a-yasui
Created December 21, 2016 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-yasui/068f1b9280f06265afa6fbf0addc65e3 to your computer and use it in GitHub Desktop.
Save a-yasui/068f1b9280f06265afa6fbf0addc65e3 to your computer and use it in GitHub Desktop.
リンク入りテキストを全部オレンジ色にしたいだけのコード。期待通り動かない泣きたい
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