目录
目录README.md

HighlightSwift 🎨

Code Syntax Highlighting in Swift and SwiftUI

CodeCardDemo

Contents

Highlight

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

CodeText

Drop-in replacement for the SwiftUI Text view

  • 🔠 Supports most Text modifiers like .font()
  • 🌗 Color styles sync automatically with Dark Mode
CodeText

CodeCard

Card view for iOS built with the CodeText view

  • 💬 Displays the detected language
  • 👆 Tap for style controls, double tap to reset
CodeCard

How to

Highlight

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:

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
}

#

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:

CodeCard(code, 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, 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.

关于
621.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号