letnotepad=Notepad(frame: view.bounds, themeFile:"one-dark")
view.addSubview(notepad)Notepad is just like any other UITextView, but you need to use the convenience initializer in order to use the themes. To create a new theme, copy one of the existing themes and edit the JSON.
Check out the Xcode project for an example. For full documentation read the code.
If you cannot work with the Notepad subclass directly for some reason, you can set up an existing UITextView or NSTextView on your own.
For iOS, you have to initialize all TextKit components yourself. Take the following as a blueprint where you can swap in custom objects:
classViewController:UIViewController{vartextView:UITextView!overridefunc viewDidLoad(){
super.viewDidLoad()letcontainerSize=CGSize(width:self.view.bounds.width, height:CGFloat.greatestFiniteMagnitude)letcontainer=NSTextContainer(size: containerSize)
container.widthTracksTextView =trueletlayoutManager=NSLayoutManager()
layoutManager.addTextContainer(container)letstorage=Storage()lettheme=Theme("one-dark")
storage.theme = theme
storage.addLayoutManager(layoutManager)leteditor=UITextView(frame:self.view.bounds, textContainer: container)
editor.backgroundColor = theme.backgroundColor
editor.tintColor = theme.tintColor
editor.autoresizingMask =[.flexibleWidth,.flexibleHeight]}}And for macOS:
classViewController:NSViewController{@IBOutletvartextView:NSTextView!letstorage=Storage()overridefunc viewDidLoad(){
super.viewDidLoad()lettheme=Theme("one-dark")
storage.theme = theme
textView.backgroundColor = theme.backgroundColor
textView.insertionPointColor = theme.tintColor
textView.layoutManager?.replaceTextStorage(storage)}}Take a look at all of the themes and swatches when choosing the theme for your Notepad, or as inspiration for a new one.
You can find all of the raw themes in the themes folder, and the file names are case-sensitive.
Using regex, you can match custom patterns in your Notepad editor by passing a regex attribute in your theme. For example, one that highlights Twitter handles in a teal color:
"handle": {
"regex": "[@@][a-zA-Z0-9_]{1,20}",
"color": "#78ddd5"
}Copy the source from the Notepad folder to your project, or add Notepad to your Podfile if you're using CocoaPods.