/TagString

TagString is a Swift struct (and a String extension) that creates NSAttributedStrings from simple markup.

Primary LanguageSwiftMIT LicenseMIT

TagString

license language Swift 4 platform iOS macOS tvOS

The TagString struct is a wrapper around strings that contain simple markup for the purpose of easily creating NSAttributedStrings.

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 &lt; and &gt; to escape angular brackets, as in HTML. &amp; escapes &.

Example

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)