Code Syntax Highlighting in Swift and SwiftUI
Converts a String
of code into a syntax highlighted AttributedString
- 🔍 Automatic language detection
- 📚 Works for 50 common languages
- 🌈 Choose from 30 classic color styles
- 🧰 Built with highlight.js and
JavaScriptCore
- 🖥️ Supported on iOS, iPadOS, macOS, and tvOS
Drop-in replacement for the SwiftUI Text
view
- 🔠 Supports most
Text
modifiers like.font()
- 🌗 Color styles sync automatically with Dark Mode
Card view for iOS built with the CodeText
view
- 💬 Displays the detected language
- 👆 Tap for style controls, double tap to reset
Converting a String
of code into a syntax highlighted AttributedString
:
let code: String = """
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
"""
let text: AttributedString = try await Highlight.text(code).attributed
The full result struct includes the detected language and other details:
let result: HighlightResult = try await Highlight.text(code)
let text: AttributedString = result.attributed
let illegal: Bool = result.illegal
let language: String = result.language
let relevance: Int32 = result.relevance
let languageName: String = result.languageName
let backgroundColor: Color = result.backgroundColor
The language:
parameter sets the language and prevents automatic detection:
let highlightResult = try await Highlight.text(code, language: "swift")
The style:
parameter changes the highlight style and color scheme:
let highlightResult = try await Highlight.text(code, style: .dark(.solarFlare))
Creating a CodeText
view with a String
of code:
let code: String = """
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
"""
var body: some View {
CodeText(code)
}
The attributed code string takes presedence over the font design, width and foreground color. Other Text
modifiers like .font()
can be used:
CodeText(code)
.font(.system(.callout, weight: .semibold))
The style:
parameter sets one of the 30 color styles.
They each have a dark variant that the CodeText
view automatically uses in Dark Mode.
CodeText(code, style: .github)
The result callback includes the detected language, background color and other details:
CodeText(code) { result in
let illegal: Bool = result.illegal
let language: String = result.language
let relevance: Int32 = result.relevance
let languageName: String = result.languageName
let attributedText: AttributedString = result.text
let backgroundColor: Color = result.backgroundColor
}
Creating a CodeCard
view with a String
of code:
let code: String = """
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
"""
var body: some View {
CodeCard(code)
}
The style:
and textStyle:
parameters can set the initially selected options:
CodeCard(code, style: .paraiso, textStyle: .caption)
- In Xcode, go to
File
>Add packages...
- Enter
https://github.com/appstefan/highlightswift
in the field and clickAdd Package
In Package.swift
add this repository as a dependency:
dependencies: [
.package(url: "https://github.com/appstefan/highlightswift.git", from: "1.0.0")
],
targets: [
.target(
name: "YourPackageName",
dependencies: ["HighlightSwift"]
)
]
Stefan, thrower_ranges.0d@icloud.com
HighlightSwift is available under the MIT license. See the LICENSE.md file.
Highlight.js is available under the BSD license. See the LICENSE.md file.