Created
December 16, 2018 19:45
-
-
Save erica/34e409057192af0f7ced64114418658c 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 AppKit | |
struct AttributableString { | |
let attributedString: NSAttributedString | |
} | |
extension AttributableString: ExpressibleByStringLiteral { | |
init(stringLiteral: String) { | |
self.attributedString = NSAttributedString(string: stringLiteral) | |
} | |
} | |
extension AttributableString: CustomPlaygroundDisplayConvertible { | |
var playgroundDescription: Any { | |
return attributedString | |
} | |
} | |
extension AttributableString: CustomStringConvertible { | |
var description: String { | |
return String(describing: self.attributedString) | |
} | |
} | |
extension AttributableString: ExpressibleByStringInterpolation { | |
init(stringInterpolation: StringInterpolation) { | |
self.attributedString = NSAttributedString(attributedString: stringInterpolation.attributedString) | |
} | |
struct StringInterpolation: StringInterpolationProtocol { | |
var attributedString: NSMutableAttributedString | |
init(literalCapacity: Int, interpolationCount: Int) { | |
self.attributedString = NSMutableAttributedString() | |
} | |
mutating func appendLiteral(_ literal: String) { | |
let astr = NSAttributedString(string: literal) | |
self.attributedString.append(astr) | |
} | |
mutating func appendInterpolation(_ string: String, _ attributes: StringAttribute...) { | |
var attributeDictionary: [NSAttributedString.Key: Any] = [:] | |
for attribute in attributes { | |
switch attribute { | |
case .backgroundColor(let color), .foregroundColor(let color): | |
attributeDictionary[attribute.key] = color | |
case .font(let font): | |
attributeDictionary[attribute.key] = font | |
case .link(let url): | |
attributeDictionary[attribute.key] = url as NSURL | |
} | |
} | |
let attributedString = NSAttributedString(string: string, attributes: attributeDictionary) | |
self.attributedString.append(attributedString) | |
} | |
} | |
} | |
public enum StringAttribute { | |
case backgroundColor(NSColor) // / UIColor, default nil: no background | |
case font(NSFont) // UIFont, default Helvetica(Neue) 12 | |
case foregroundColor(NSColor) // UIColor, default blackColor | |
case link(NSURL) // // NSURL (preferred) or NSString | |
} | |
extension StringAttribute { | |
public var key: NSAttributedString.Key { | |
switch self { | |
case .backgroundColor: return NSAttributedString.Key.backgroundColor | |
case .font: return NSAttributedString.Key.font | |
case .foregroundColor: return NSAttributedString.Key.foregroundColor | |
case .link: return NSAttributedString.Key.link | |
} | |
} | |
} | |
let bigFont = NSFont.boldSystemFont(ofSize: 64) | |
let futura = NSFont(name: "Futura", size: 18)! | |
let s = AttributableString(""" | |
Hello \("world", .foregroundColor(.red))! | |
\("This is big", .font(bigFont), .backgroundColor(.lightGray), .foregroundColor(.green)) | |
\("The end", .font(futura)) | |
""") | |
s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment