Highlightr is an iOS & OSX syntax highlighter built with Swift. It uses highlight.js as it core, supports 166 languages and comes with 77 styles.
Takes your lame string with code and returns a NSAttributtedString with proper syntax highlighting.
The current version is under development but is functional.
Highlightr provides two main classes:
This is the main endpoint, you can use it to convert code strings into NSAttributted strings.
let highlightr = Highlightr()
highlightr.setTheme("paraiso-dark")
let code = "let a = 1"
let highlightedCode = highlightr.highlight("swift", code: code, fastRender: true)
A sublcass of NSTextStorage, you can use it to highlight text on real time.
let textStorage = CodeAttributedString()
textStorage.language = "Swift"
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: view.bounds.size)
layoutManager.addTextContainer(textContainer)
let textView = UITextView(frame: yourFrame, textContainer: textContainer)
Yes, Highlightr relies on iOS & OSX JavaScriptCore to parse the code using highlight.js. This is actually quite fast!
It will never be as fast as a native solution, but it's fast enough to be used on a real time editor.
It comes with a custom made HTML parser for creating NSAttributtedStrings, is pre-processing the themes and is preloading the JS libraries. As result it's taking around of 50 ms on my iPhone 6s for processing 500 lines of code.
Highlightr is available under the MIT license. See the LICENSE file for more info.
Highlight.js is available under the BSD license. You can find the license file here.