Markup generates attributed strings using a familiar markup syntax:
For example, the following text:
The *quick*, ~red~ brown fox jumps over a _*lazy dog*_.
will be formatted like this:
The quick, red brown fox jumps over a lazy dog.
Just to give you an idea, here is a screenshot of the sample application displaying the markup text and the resulting attributed string:
Render an attributed string
You can use MarkupRenderer
to generate an attributed string from a given markup text:
import Markup
let renderer = MarkupRenderer(baseFont: .systemFont(ofSize: 16))
let attributedText = renderer.render(text: "The *quick*, ~red~ brown fox jumps over a _*lazy dog*_.")
Access the markup syntax tree
Use MarkupParser
to generate an abstract syntax tree for a markup text:
let nodes = MarkupParser.parse(text: "The *quick*, ~red~ brown fox jumps over a _*lazy dog*_")
dump(nodes)
// Outputs:
[
.text("The "),
.strong([
.text("quick")
]),
.text(", "),
.delete([
.text("red")
]),
.text(" brown fox jumps over a "),
.emphasis([
.strong([
.text("lazy dog")
])
])
]
Both the parsing and the rendering will take linear time to complete.
This post explains how Markup internally works, in case you are curious about the implementation.
Using the Swift Package Manager
Add Markup as a dependency to your Package.swift
file. For more information, see the Swift Package Manager documentation.
.package(url: "https://github.com/gonzalezreal/Markup", from: "2.3.0")
Using Carthage
Add github "gonzalezreal/Markup"
to your Cartfile
Using CocoaPods
Add pod Markup
to your Podfile
Markup
.link |
Stars: 95 |
Last commit: 4 years ago |
Add proper support for Swift Package Manager and Xcode 11 / Swift 5.1
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics