Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeEditorView

A lightweight Swift package for displaying syntax-highlighted code on iOS. Built on CoreText and Tree-sitter, it renders code with a line-number gutter and supports text selection. Currently it doesn't support editing the text.

Simulator Screenshot

Requirements

  • iOS 15+
  • Swift 5.9+

Then add "CodeEditorView" to your target's dependencies.

Usage

SwiftUI

import SwiftUI
import CodeEditorView

struct ContentView: View {
    let code = "let x = 42"

    var body: some View {
        CodeEditor(code: code, language: try? .swift())
            .wordWrap()
            .theme(.default)
    }
}

UIKit

import UIKit
import CodeEditorView

let editorView = CodeEditorView()
editorView.text = NSAttributedString(string: "let x = 42")
editorView.wordWrap = true
view.addSubview(editorView)

Supported Languages

Language Configuration
Swift .swift()
Python .python()
JSON .json()
CSS .css()
HTML .html()
Go .golang()

Pass nil as the language to display plain text without highlighting.

Theming

SyntaxTheme controls font and token colors. Use the built-in default or create your own:

let theme = SyntaxTheme(
    baseFont: .monospacedSystemFont(ofSize: 14, weight: .regular),
    baseColor: .label,
    captureColors: [
        "keyword": .systemPink,
        "string":  .systemOrange,
        "comment": .systemGreen,
    ]
)

CodeEditor(code: code, language: try? .python())
    .theme(theme)

Common capture names: keyword, string, number, comment, function, type, variable, property, operator, tag, attribute, escape.

About

A UIKit/SwiftUI editor that supports syntax highlighting

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages