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, which is now no longer maintained.
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. Please note that some themes have been modified slightly to meet the needs of another project. Sepcifically:
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() {
...
}
You can set a specific theme using the setTheme()
function:
highlighter.setTheme("atom-one-light")
You can also apply your chosen font at this time too in place of the default: 14pt Courier.
highlighter.setTheme("atom-one-light",
withFont: "Menlo-Regular",
ofSize: 16.0)
You can also specify a line spacing value:
highlighter.theme.lineSpacing = self.lineSpacing * self.fontSize
and/or a paragraph spacing value:
highlighter.theme.paraSpacing = 1.0
A value of 0.0
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.
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) ?? NSFont.systemFont(ofSize: 16.0)
highlighter.theme.setCodeFont(font)
Finally, get an optional NSAttributedString containing the formatted code:
if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift") {
myTextView.textStorage!.addAttributedString(displayString)
}
The second parameter of highlight()
is the name of language you’re rendering. If you leave out this parameter, or pass nil
, HighlighterSwift will use the language detection feature built into Highlight.js.
From 2.0.0, pass in a fourth parameter, an instance of a LineNumberingData
structure, to instruct HighlighterSwift to add line numbers to the code according to the values included in this data. The default for this parameter is nil
(don’t add line numbers).
public struct LineNumberData {
public var numberStart: Int = 1
public var minWidth: Int = 2
public var separator: String = " "
public var usingDarkTheme: Bool = false
public var lineBreak: String = "\n"
public var fontSize: CGFloat = 16.0
}
LineNumberingData
properties allow you to specify:
001
) but there are a thousand or more lines in the code, the first line will be rendered as 0001
.false
.\n
. Note You should not need to change this.All these values are optional.
Use this feature in this way:
var lineNumberingData = LineNumberData()
lineNumberingData.minWidth = 4
lineNumberingData.numberStart = 100
lineNumberingData.usingDarkTheme = !isMacInLightMode()
lineNumberingData.fontSize = self.settings.fontSize
if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift", lineNumbering: lineNumberingData) {
myTextView.textStorage!.addAttributedString(displayString)
}
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.
You can view HighlighterSwift’s source code at GitHub.
@greggreg
)