Highlightr is an iOS & macOS syntax highlighter built with Swift. It uses highlight.js as it core, supports 185 languages and comes with 89 styles.
Takes your lame string with code and returns attributtes syntax highlighting.
- iOS 10.0+
- macOS 10.10+
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsTo integrate Highlightr into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target '<Your Target Name>' do
pod 'Highlightr3'
endThen, run the following command:
$ pod installThe Swift Package Manager automates the distribution of Swift code. To use Highlightr with SPM, add a dependency to your Package.swift file:
let package = Package(
dependencies: [
.package(url: "https://github.com/octree/Highlightr.git", ...)
]
)Highlightr provides two main classes:
let highlightr = Highlightr()
highlightr.setTheme(to: "dracula")
let code = "let a = 1"
// You can omit the second parameter to use automatic language detection.
let highlightedCode = highlightr.highlight(code, as: "swift")
A subclass of NSTextStorage, you can use it to highlight text on real time.
let textStorage = HighlightTextStorage()
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 & macOS 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's taking around of 7.4ms on my iPhone iPhone Xs Max for processing 100 lines of swift 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.
