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))
#
CodeText
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:
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
}
#
CodeCard
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:
HighlightSwift 🎨
Code Syntax Highlighting in Swift and SwiftUI
Contents
Highlight
Converts a
String
of code into a syntax highlightedAttributedString
JavaScriptCore
CodeText
Drop-in replacement for the SwiftUI
Text
viewText
modifiers like.font()
CodeCard
Card view for iOS built with the
CodeText
viewHow to
Highlight
Converting a
String
of code into a syntax highlightedAttributedString
:The full result struct includes the detected language and other details:
The
language:
parameter sets the language and prevents automatic detection:The
style:
parameter changes the highlight style and color scheme:#
CodeText
Creating a
CodeText
view with aString
of code:The attributed code string takes presedence over the font design, width and foreground color. Other
Text
modifiers like.font()
can be used:The
style:
parameter sets one of the 30 color styles. They each have a dark variant that theCodeText
view automatically uses in Dark Mode.The result callback includes the detected language, background color and other details:
#
CodeCard
Creating a
CodeCard
view with aString
of code:The
style:
andtextStyle:
parameters can set the initially selected options:Installation
Project
File
>Add packages...
https://github.com/appstefan/highlightswift
in the field and clickAdd Package
Package
In
Package.swift
add this repository as a dependency:Author
Stefan, thrower_ranges.0d@icloud.com
License
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.