Contents

CodeEditApp/CodeEditTextView

A text editor specialized for displaying and editing code documents. Written in pure Swift.

Documentation

This package is fully documented here.

Usage

This package exports a primary TextView class. The TextView class is an NSView subclass that can be embedded in a scroll view or used standalone. It parses and renders lines of a document and handles mouse and keyboard events for text editing. It also renders styled strings for use cases like syntax highlighting.

import CodeEditTextView
import AppKit

/// # ViewController
/// 
/// An example view controller for displaying a text view embedded in a scroll view.
class ViewController: NSViewController, TextViewDelegate {
    private var scrollView: NSScrollView!
    private var textView: TextView!
    
    var text: String = "func helloWorld() {\n\tprint(\"hello world\")\n}"
    var font: NSFont!
    var textColor: NSColor!
    
    override func loadView() {
		textView = TextView(
            string: text,
            font: font,
            textColor: textColor,
            lineHeightMultiplier: 1.0,
            wrapLines: true,
            isEditable: true,
            isSelectable: true,
            letterSpacing: 1.0,
            delegate: self
        )
        textView.translatesAutoresizingMaskIntoConstraints = false

        scrollView = NSScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.hasVerticalScroller = true
        scrollView.hasHorizontalScroller = true
        scrollView.documentView = textView
        self.view = scrollView
		NSLayoutConstraint.activate([
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        textView.updateFrameIfNeeded()
    }
}

License

Licensed under the MIT license.

Dependencies

Special thanks to Matt Massicotte for the great work he's done!

| Package | Source | Author | | :---------- | :--------------------------------------------------- | :-------------------------------------------- | | TextStory | GitHub | Matt Massicotte | | swift-collections | GitHub | Apple |

Package Metadata

Repository: CodeEditApp/CodeEditTextView

Homepage: https://codeeditapp.github.io/CodeEditTextView/documentation/codeedittextview/

Stars: 174

Forks: 36

Open issues: 10

Default branch: main

Primary language: swift

License: MIT

Topics: appkit, coretext, macos, swift, text-editor

README: README.md