Highlightr Documentation

repository·master·Indexed 23 days ago

https://github.com/raspu/highlightr

An iOS and macOS syntax highlighter built with Swift that uses highlight.js via JavaScriptCore. It provides the Highlightr class for converting code strings into NSAttributedString objects and a CodeAttributedString subclass of NSTextStorage for real-time editing. Supports iOS 8.0+ and macOS 10.10+.

Tokens
665
Snippets
2
Records
4
Agent score
34%

What's inside Highlightr

  1. Install Highlightr via Swift Package Manager (SPM)

    master

    To install Highlightr in your iOS or macOS project using Swift Package Manager:

    1. Click the File menu and select Add Packages…
    2. Enter https://github.com/raspu/Highlightr/ into the Search or Enter Package URL field.
    3. Link Highlightr to your application target:
      • Select your app target.
      • On the General tab, scroll down to Frameworks, Libraries, and Embedded Content.
      • Click +, select Add Other..., then Add Package Dependency...
    4. Add import Highlightr to your source files.
  2. Use the Highlightr class for syntax highlighting

    master

    The Highlightr class is the primary entry point for converting code strings into NSAttributedString objects. You can specify a theme and a language, or omit the language parameter to use automatic language detection.

    Note: Highlightr is no longer actively maintained. The author recommends using HighlighterSwift instead.

    let highlightr = Highlightr()
    highlightr.setTheme(to: "paraiso-dark")
    let code = "let a = 1"
    // You can omit the second parameter to use automatic language detection.
    let highlightedCode = highlightr.highlight(code, as: "swift") 
  3. Use CodeAttributedString for real-time syntax highlighting

    master

    The CodeAttributedString class is a subclass of NSTextStorage. It is designed for scenarios where you need to highlight text in real time, such as in a text editor. You must integrate it with an NSLayoutManager and an NSTextContainer to work with a UITextView or NSTextView.

    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)
  4. Highlightr Requirements and Compatibility

    master

    Highlightr requires the following minimum versions:

    • iOS: 8.0+
    • macOS: 10.10+

    Important Compatibility Notes:

    • CocoaPods & Carthage: These are no longer supported.
    • Engine: Highlightr relies on JavaScriptCore to parse code using highlight.js.