TextKit2: Strange behaviour in directional text selection navigation

Hi there,

I am currently trying to extend the TextKit 2 demo project from WWDC 2021 to allow keyboard input under iOS using UITextInteraction. For the most part, it is working well, but the behaviour of the directional cursor text navigation is strange: When moving up or down, it keeps the horizontal location within the current paragraph. But when crossing an empty line into another paragraph, the former horizontal position is lost and the cursor is reset to the line start. This is a video showing the behaviour: https://www.icloud.com/iclouddrive/0Xwf9hpb2DcAoU1CNPZnVgwQw#TextKit2_Selection_Bug

I would expect the cursor to restore its former horizontal position in following paragraphs. This is the standard behaviour in UITextView and NSTextView.

Does anybody know if this a bug in TextKit2 or if there is a way to achieve the expected beahviours.

Thanks

Frank

I'm sure it's a bug, the TextEdit app in macOS 12.0.1 maintains selection properly for the very same RTF file. The NSAttributedString itself looks ok at first glance:

There's another interesting and unexpected (at least to me) behavior when setting the NSTextContentStorage.attributedString directly, the cursor (nor selection) is available at all and NSTextView is not editable

            let docURL = Bundle.main.url(forResource: "Text", withExtension: "rtf")!
            let attributedString = try! NSAttributedString(rtf: Data(contentsOf: docURL), documentAttributes: nil)
            try! textContentStorage.attributedString = attributedString
TextKit2: Strange behaviour in directional text selection navigation
 
 
Q