使用方法
let notepad = Notepad(frame: view.bounds, themeFile: "one-dark")
view.addSubview(notepad)
Notepad 就像任何其他的 UITextView 一样,但您需要使用便利初始化器来使用主题。要创建一个新的主题,复制现有的主题并编辑 JSON。
查看 Xcode 项目 以获取 示例。对于完整文档,请阅读 代码.
扩展现有文本视图以包含 Notepad 功能
如果您无法直接使用 Notepad
子类,您可以自己设置现有的 UITextView
或 NSTextView
。
对于 iOS,您必须自己初始化所有 TextKit 组件。以下是一个蓝图,您可以在此处替换自定义对象
class ViewController: UIViewController {
var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let containerSize = CGSize(width: self.view.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let container = NSTextContainer(size: containerSize)
container.widthTracksTextView = true
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(container)
let storage = Storage()
let theme = Theme("one-dark")
storage.theme = theme
storage.addLayoutManager(layoutManager)
let editor = UITextView(frame: self.view.bounds, textContainer: container)
editor.backgroundColor = theme.backgroundColor
editor.tintColor = theme.tintColor
editor.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
而对于 macOS。
class ViewController: NSViewController {
@IBOutlet var textView: NSTextView!
let storage = Storage()
override func viewDidLoad() {
super.viewDidLoad()
let theme = Theme("one-dark")
storage.theme = theme
textView.backgroundColor = theme.backgroundColor
textView.insertionPointColor = theme.tintColor
textView.layoutManager?.replaceTextStorage(storage)
}
}
主题
在选择 Notepad 的主题或作为创建新主题的灵感来源时,查看所有 主题和色板。
您可以在 主题文件夹 中找到所有原始主题,且文件名是大小写敏感的。
自定义正则表达式
使用正则表达式,你可以在记事本编辑器中通过传递一个 regex
属性来匹配自定义模式。例如,可以高亮显示蓝色颜色的推特账号。
"handle": {
"regex": "[@@][a-zA-Z0-9_]{1,20}",
"color": "#78ddd5"
}
安装
将记事本的源文件复制到你的项目,或者如果你使用 CocoaPods,请将记事本添加到你的 Podfile。