Skip to content

Instantly share code, notes, and snippets.

@gregomni
Created October 11, 2017 18:05
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 gregomni/49ecc35be15cd28d400cf4da4cb42397 to your computer and use it in GitHub Desktop.
Save gregomni/49ecc35be15cd28d400cf4da4cb42397 to your computer and use it in GitHub Desktop.
import Cocoa
enum NSAttributedStringAttribute {
case font(NSFont)
case foregroundColor(NSColor)
// ...
static func getDictionary(_ attributes: [NSAttributedStringAttribute]) -> [NSAttributedStringKey: Any] {
var result: [NSAttributedStringKey: Any] = [:]
for attribute in attributes {
switch attribute {
case .font(let font):
result[.font] = font
case .foregroundColor(let color):
result[.foregroundColor] = color
}
}
return result
}
}
extension NSAttributedString {
convenience init(string: String, attributes: [NSAttributedStringAttribute]) {
self.init(string: string, attributes: NSAttributedStringAttribute.getDictionary(attributes))
}
}
let displayName = "foo"
let attributes = [NSAttributedStringAttribute.font(NSFont.systemFont(ofSize: 12))]
let attributedString = NSAttributedString(string: displayName, attributes: attributes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment