RPSyntaxHighlighter

A regexp-powered, NSAttributedString-backed syntax highlighting library for iOS


Project maintained by rpwll Hosted on GitHub Pages — Theme by mattgraham

A regexp-powered, NSAttributedString-backed syntax highlighting library for iOS

Note: RPSyntaxHighlighter is currently still in development and not yet ready for use in your code. In the future I'd like to do a few things:

A screenshot of the sample app

RPSyntaxHighlighter is a easy to use, fully extensible syntax highlighting library for iOS. It was built with a few ideas in mind:

  1. Ease of use: it should be easy to highlight and process code in your app. This is why we use NSAttributedString objects which can be displayed in most UIKit controls as of iOS 6.
  2. Extensibility: The language definitions and themes used by RPSyntaxHighlighter are simple JSON files. This means it's easy to add extra languages or design a theme that matches your app.
  3. Simplicity: The entire RPSyntaxHighlighter codebase consists of three classes, meaning it's easy to understand what's going on. It's also fully documented with appledoc.

Using RPSyntaxHighlighter

The easiest way to use RPSyntaxHighlighter is by calling the highlightCode:withLanguage method on it. This single method covers 90% of the most common use cases. If you want to do anything more complicated, the class headers are all thoroughly documented and serve as a good starting point.

Language Definitions

A simple language definition looks like so:

[
    {
      "scopes" : ["function", "call"],
      "pattern" : "(\\w+?)(?=\\()"
    },
    {
      "scopes" : ["constant", "language"],
      "pattern" : "(true|false|null)(?i)"
    }
]

As should be immediately obvious, a language definition is nothing more than a JSON document containing an array of objects. Each of these objects consists of a regular expression pattern and an array of scopes to be applied to the matched section of code. The scopes applied to a section of code are effectively tags used to tell the highlighter how to style a section of code.

The array of scopes are applied to a section of code in order, as such you should specify them in order from least to most specific such that more specific styles override simpler ones. However, the order of matchers in the document should be from highest to lowest precedence.

Theme Definitions

Here's an exceprt taken from the included tomorrow-night theme:

{
  "default" : {
    "color" : "E9E2CB",
    "background" : "1C1F21",
    "font" : "DejaVuSansMono",
    "fontSize" : 14.0
  },
  "keyword" : {
    "color" : "B193BA"
  },
  "constant" : {
    "color" : "DD935E"
  },
  "string" : {
    "color" : "B5BC67"
  }
}

As with language definitions, themes are simple JSON documents consisting of a single dictionary. Each key in the dictionary corresponds to a scope in the code to be highlighted with the exception of default which applies to the document as a whole.

The value associated with these keys is another dictionary that can have any of the following properties:

Note that any fonts you wish to use must be included in the main bundle of your app.