I encountered some issues with rendering in NSTextView
. When I'm hitting enter the last line rendering is lagging and the line is not visible. Also, sometimes when I hit backspace the last line is temporarily duplicated.
The problem is presented on the video: here.
Code to reproduce the issue:
func bug() {
let textContainer = NSTextContainer()
textContainer.widthTracksTextView = true
textContainer.heightTracksTextView = true
let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage(string: "")
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
let textView = NSTextView(frame: .zero, textContainer: textContainer)
textView.autoresizingMask = [.width, .height]
// The case that presents the bug:
textView.string = "\naaaaa\nbbbbbb"
textView.setSelectedRange(.init(location: 0, length: 0))
let scrollView = NSScrollView()
scrollView.documentView = textView
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(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)
])
}
In general, it looks like it is a problem with TextKit 1. If I use TextKit 2, it works fine:
func textKit2NoBug() {
let textView = NSTextView(usingTextLayoutManager: true)
textView.autoresizingMask = [.width, .height]
// The case that presents the bug:
textView.string = "\naaaaa\nbbbbbb"
textView.setSelectedRange(.init(location: 0, length: 0))
let scrollView = NSScrollView()
scrollView.documentView = textView
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(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)
])
}
However, in my case, I have to rely on TextKit 1. Does anyone know how to workaround this issue?
Full source code: here