Created
October 11, 2017 18:05
-
-
Save gregomni/49ecc35be15cd28d400cf4da4cb42397 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
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