Latest 2.0-beta.1
Homepage https://github.com/gonzalezreal/Markup
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0
Frameworks Foundation
Authors

CocoaPods
Carthage Compatible
Platforms

Markup generates attributed strings using a familiar markup syntax:

  • To emphasize words or sentences, you can surround the text with *asterisks* to create bold text or _underscores_ for italic text.
  • To show corrections in the text, surround the text with ~tildes~ to strike out the text.
  • You can combine formatting options.

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:

Screenshot

Examples

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")
    ])
  ])
]

Performance

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.

Installation

Using CocoaPods

Add pod Markup to your Podfile

Using Carthage

Add git "gonzalezreal/Markup" to your Cartfile

Using the Swift Package Manager

Add Package(url: "https://github.com/gonzalezreal/Markup.git", majorVersion: 1) to your Package.swift file.

Help & Feedback

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to Markup.
  • Contact @gonzalezreal on Twitter.

Latest podspec

{
    "name": "Markup",
    "version": "2.0-beta.1",
    "summary": "Lightweight markup text formatting in Swift",
    "description": "Markup generates attributed strings using a familiar markup syntax:nn* To emphasize words or sentences, you can surround the text with *asterisks* to create bold text or _underscores_ for italic text.n* To show corrections in the text, surround the text with ~tildes~ to strike out the text.n* You can combine formatting options.",
    "homepage": "https://github.com/gonzalezreal/Markup",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Guille Gonzalez": "[email protected]"
    },
    "social_media_url": "https://twitter.com/gonzalezreal",
    "screenshots": "https://raw.githubusercontent.com/gonzalezreal/Markup/master/MarkupExample/Screenshot.png",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/gonzalezreal/Markup.git",
        "tag": "2.0-beta.1"
    },
    "source_files": "Sources/**/*",
    "frameworks": "Foundation",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This