This example starts with Apple's standard SwiftUI app template and adds a
CodeViewer backed by every grammar in CodeViewerKitStandardLanguages.
-
In Xcode choose File > New > Project, then Multiplatform > App.
-
Choose SwiftUI for Interface, Swift for Language, and None for Storage.
-
Set the deployment targets to iOS/iPadOS 18 or newer and macOS 15 or newer.
-
Choose File > Add Package Dependencies and enter:
https://github.com/cavs1910/CodeViewerKitStandardLanguages.git -
Add the
CodeViewerKitStandardLanguagesproduct to the app target. It suppliesCodeViewerKitand the language modules transitively. -
Drag
lorem-ipsum-html.txtinto the project, enable Copy items if needed, and include it in the app target. -
Replace the two files generated by Xcode with
CodeViewerExampleApp.swiftandContentView.swift. Rename the app type if your project uses a different name.
The store is owned by ContentView so it survives SwiftUI body updates and
reuses completed highlighting. A larger app can instead create one store at
its root and pass it to each feature that displays code.
CodeViewerCommands adds Command-Plus and Command-Minus while a viewer is
focused. The same source files compile for macOS, iPhone, and iPad.
ContentView selects ExampleDocument.bundledHTML by default. It reads the
roughly 500-line HTML sample from the app bundle and explicitly highlights it
as HTML even though the resource uses a .txt extension. Change that one
assignment to ExampleDocument.inlineSwift to use the inline Swift string
instead. This keeps file-backed and in-memory source loading visible in the
same example.