A Swift wrapper for the popular Highlight.js syntax highlighter

Latest Release: 1.1.5


This library provides a Swift wrapper for the popular Highlight.js code highlighting utility.

It is a more up-to-date version of Juan Pablo Illanes’ Highlightr library and relies heavily upon code from that project, which is now no longer maintained.

Various PreviewCode themes

How to use HighlighterSwift


To add HighlighterSwift to your project, use Xcode’s File > Swift Packages menu command. When requested, enter this repo’s git URL, https://github.com/smittytone/HighlighterSwift.git.

The library already contains the Highlight.js code and themes.

Note This project was begun to support another, so some themes have been modified slightly to meet the needs of that other project. For example, background images have been removed from the Brown Paper, Greyscale, Schoolbook and Pojoacque themes; the two Kimbie themes have been renamed for consistency; and colours have been formalised as hex values.


Make sure Highlighter is listed under Frameworks, Libraries, and Embedded Content under your target’s General settings.

Include import Hightlighter at the top of your source code.

Instantiate a Highlighter object. Its init() function returns an optional, which will be nil if the Highlight.min.js file could not be found or is non-functional, or the Default theme CSS file is missing:

if let highlighter: Highlighter = Highlighter.init() {

You can set a specific theme using the setTheme() function:


You can also apply your chosen font at this time too, or use the default: 14pt Courier.

                     withFont: "Menlo-Regular",
                     ofSize: 16.0)

From 1.1.3, you can also specify a line spacing value:

highlighter.theme.lineSpacing = (self.lineSpacing - 1.0) * self.fontSize

and/or a paragraph spacing value:

highlighter.theme.paraSpacing = 1.0

A value of zero for lineSpacing is equivalent to single spacing. paraSpacing is the space in points added at the end of the paragraph — use 0.0 for no additional spacing (the default).

Both values must be non-negative. Negative values be replaced with default values: 0.0 in both cases.

Note These new values are applied to the instance’s theme property.

You can set or change your preferred font later by using setCodeFont(), which takes an NSFont or UIFont instance configured for the font and text size you want, and is called on the Highlighter instance’s theme property:

let font: NSFont = NSFont.init(name: "Menlo-Regular",
                               size: 16.0)!

Finally, get an optional NSAttributedString containing the formatted code:

if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift") {

The second parameter of highlight() is the name of language you’re rendering. If you leave out this parameter, or pass nil, Highlighter will use Highlight.js’ language detection feature.

You can get a list of supported languages by the name they are known to Highlight.js by calling supportedLanguages() — it returns an array of strings.

The function availableThemes() returns a list of the installed themes.


PreviewCode makes use of code from the following sources:

Source Code

You can view HighlighterSwift’s source code at GitHub.

Release Notes

  • 1.1.5 29 April 2024
    • Add dark GitHub theme.
  • 1.1.4 10 November 2023
    • Update to Highlight.js 11.9.0.
    • Fix: add missing iOS-oriented import (thanks @greggreg)
  • 1.1.3 16 May 2023
    • Add line and paragraph spacing controls.
  • 1.1.2 15 March 2023
    • Add missing languages.
  • 1.1.1 14 March 2023
    • Update to Highlight.js 11.7.0.
  • 1.1.0 26 April 2022
    • Update to Highlight.js 11.5.0.
    • Include all Highlight.js languages.
  • 1.0.0 17 June 2021
    • Initial public release.

Related Software

Site and software copyright © 2024, Tony Smith