For some reason the code above doesn't reproduce the issue anymore. Here is new code that seems to work (for now):
class ViewController: NSViewController {
@IBOutlet var textView: NSTextView!
override func viewDidAppear() {
textView.textContainerInset = CGSize(width: 0, height: 8)
let _ = textView.layoutManager
textView.textStorage!.setAttributedString(NSAttributedString(string: "1\n\n2\n3\n4"))
textView.textStorage!.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSRange(location: 0, length: textView.textStorage!.length))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 0, length: 2))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 1, length: 2))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 3, length: 2))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 5, length: 2))
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [self] in
textView.selectedRange = NSRange(location: 3, length: 0)
textView.deleteBackward(nil)
textView.textStorage!.beginEditing()
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 0, length: 2))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 2, length: 2))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 4, length: 2))
textView.textStorage!.addAttribute(.paragraphStyle, value: paragraphStyle(indent: 100), range: NSRange(location: 6, length: 1))
textView.textStorage!.endEditing()
}
}
private func paragraphStyle(indent: Double) -> NSParagraphStyle {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = indent
return paragraphStyle
}
}
Topic:
UI Frameworks
SubTopic:
AppKit
Tags: