Skip to content

Instantly share code, notes, and snippets.

@erica
Created December 16, 2018 19:45
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 erica/34e409057192af0f7ced64114418658c to your computer and use it in GitHub Desktop.
Save erica/34e409057192af0f7ced64114418658c to your computer and use it in GitHub Desktop.
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