My app uses TextKit 1 and unfortunately still cannot migrate to TextKit 2 because of some bugs (for instance in FB17103305 I show how NSTextView.shouldDrawInsertionPoint has no effect, but I opened that feedback exactly one year ago and it still has no answer). Unfortunately TextKit 1 has another bug which causes the text cursor to jump unpredictably up or down when pressing the arrow keys and setting UITextView.typingAttributes.
Run the code below on iPhone 17 Pro Max Simulator.
Scroll the text down until you see “Header 2”.
Place the text cursor after “# “.
Press the arrow down key twice to move the cursor two lines down. The cursor moves to the top of the view instead. Continuing to press the arrow keys up and down results in the cursor sometimes moving as expected, other times jumping around wildly.
Does anyone know a workaround?
I created FB22382453.
class TextView: UITextView, UITextViewDelegate {
override func awakeFromNib() {
let _ = layoutManager
delegate = self
let header = textAttributes(fontSize: 30)
let body = textAttributes(fontSize: 15)
let string = NSMutableAttributedString(string: String(repeating: "a", count: 2681) + "\n", attributes: body)
string.append(NSAttributedString(string: """
# Header 1
""", attributes: header))
string.append(NSMutableAttributedString(string: String(repeating: "a", count: 5198) + "\n", attributes: body))
string.append(NSAttributedString(string: """
# Header 2
""", attributes: header))
string.append(NSMutableAttributedString(string: String(repeating: "a", count: 7048) + "\n", attributes: body))
textStorage.setAttributedString(string)
}
func textViewDidChangeSelection(_ textView: UITextView) {
typingAttributes = textStorage.attributes(at: selectedRange.location - 1, effectiveRange: nil)
}
private func textAttributes(fontSize: Double) -> [NSAttributedString.Key: Any] {
var textAttributes = [NSAttributedString.Key: Any]()
textAttributes[.font] = UIFont(name: "Courier", size: fontSize)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.minimumLineHeight = round(fontSize * 1.3)
paragraphStyle.maximumLineHeight = paragraphStyle.minimumLineHeight
textAttributes[.paragraphStyle] = paragraphStyle
return textAttributes
}
}
0
0
21