HighlightSwift ?

Syntax Highlighting in Swift and SwiftUI

CodeCardDemo

Contents

Highlight

Convert any 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

CodeText

Drop-in replacement for the SwiftUI Text view

  • ⬜️ Clear or style-colored background
  • ? Supports most Text modifiers like .font()
  • ? Color style syncs automatically with Dark Mode

CodeText

CodeCard

Simple iOS card view built with the CodeText view

  • ? Displays the detected language
  • ? Tap for style controls, double tap to reset

CodeCard

How to use

Highlight

Convert a String of code into a syntax highlighted AttributedString:

let text: String = """
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)
"""
    
let result = try await Highlight.code(text)
let attributedText: AttributedString = result.text

The result also includes the detected language and other details:

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

Use the language: parameter to skip automatic detection:

let highlightResult = try await Highlight.code(text, language: "swift")

Use the style: parameter to set the highlight style and color scheme:

let highlightResult = try await Highlight.code(text, style: .dark(.solarFlare))

CodeText

Create a CodeText view with a String of code:

let text: String = """
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)
"""

var body: some View {
    CodeText(text)
}

Most Text modifiers like .font() work as normal, just the font design and width cannot be changed:

CodeText(text)
    .font(.system(.callout, weight: .semibold))

Use the style: and showBackground: parameters to adjust the appearance:

CodeText(text, style: .atomOne, showBackground: true)
    .font(.body)

The result callback includes the detected language and other details:

CodeText(text) { 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

Create a CodeCard with a String of code:

let text: String = """
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)
"""

var body: some View {
    CodeCard(text)
}

Use the style: and textStyle: parameters to adjust the initial appearance:

CodeCard(text, style: .paraiso, textStyle: .caption)

Installation

Project

  1. In Xcode, go to File > Add packages...
  2. Enter https://github.com/appstefan/highlightswift in the field and click Add Package

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"]
    )
]

Author

Stefan, [email protected]

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.

GitHub

View Github