I can't get NSTextView to work in textkit2 with the following code, textView.textContentStorage
, textContentStorage.textStorage
is both nil. does anyone have a working example? hope there is a Frameworks Engineer help me out, appreciate so much.
import AppKit
open class MyEditor: NSView {
let textView: NSTextView
let textLayoutManager: NSTextLayoutManager
let textContentStorage: NSTextContentStorage
public init() {
textLayoutManager = NSTextLayoutManager()
textContentStorage = NSTextContentStorage()
let textContainer = NSTextContainer(size: NSSize(width: 200, height: 0.0))
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
textLayoutManager.textContainer = textContainer
textContentStorage.addTextLayoutManager(textLayoutManager)
textView = NSTextView(frame: .zero, textContainer: textContainer)
super.init(frame: .zero)
addSubview(textView)
}
public func setAttributedString(_ attrString: NSAttributedString) {
print(textView.textContentStorage) // always nil
textContentStorage.textStorage?.setAttributedString(attrString)
}
public required init?(coder: NSCoder) {
return nil
}
}