CodeEditTextView

一个专门用于显示和编辑代码文档的文本编辑器。功能包括基本文本编辑、极快的初始布局、支持处理大型文档、以及代码文档的自定义选项。

GitHub release Github Tests Documentation GitHub Repo stars GitHub forks Discord Badge

重要提示

此软件包包含一个文本视图,适用于在某些特定情况下替换 NSTextView。如果您需要一个可以处理诸如:从右到左的文本、自定义布局元素或与系统文本视图功能对等的文本视图,请考虑使用 STTextViewNSTextView。此库导出的 TextView 旨在布局由文本行组成的文档。它也不试图推断文档的内容。如果您正在寻找编辑源代码(缩进、语法高亮),请考虑使用父库 CodeEditSourceEditor

文档

此软件包已在此处完整记录:here

用法

此软件包导出一个主要的 TextView 类。TextView 类是一个 NSView 子类,可以嵌入到滚动视图中或单独使用。它解析和渲染文档的行,并处理用于文本编辑的鼠标和键盘事件。它还渲染样式化字符串,用于诸如语法高亮之类的用例。

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()
    }
}

许可证

根据 MIT 许可证 授权。

依赖项

特别感谢 Matt Massicotte 的杰出工作!

软件包 来源 作者
TextStory GitHub Matt Massicotte
swift-collections GitHub Apple

相关仓库

        CodeEdit        

CodeEditSourceEditor

     CodeEditKit     

CodeEditLanguages

    CodeEdit CLI