The TagString
struct is a wrapper around strings that contain simple markup for the purpose of easily creating NSAttributedString
s.
Tags can be nested. Attributes take precedence inside and out.
Tags are written with angular brackets <tag>
. Closing tag names begin with a forward slash </tag>
. Use <
and >
to escape angular brackets, as in HTML. &
escapes &.
Using TagString struct:
let attributes: [String: [NSAttributedStringKey: AnyObject]] = [
"loud": [.font: UIFont.systemFont(ofSize: 40)],
"green": [.color: UIColor.green]
]
let string: TagString = "Testing <loud>this <green>text</green></loud> thing."
string.attributed(with: attributes)
Using String extension:
let attributes: [String: [NSAttributedStringKey: AnyObject]] = [
"loud": [.font: UIFont.systemFont(ofSize: 40)],
"green": [.color: UIColor.green]
]
let string = "Testing <loud>this <green>text</green></loud> thing.".attributed(with: attributes)