I'm adding some text formatting support to an editable text view in my app, and I want to include bulleted lists. I specifically want to have the standard behavior where if you're typing a line of text in a list and press Enter, the next bullet item is automatically inserted.
This appears to somewhat work out-of-the-box with NSTextList
on iOS 16, unless the line you're editing is at the very end of the document. This is apparent in the sample code for WWDC 2022 session 10090 (project is here):
- Run that sample app and switch to the List tab
- Put your cursor at the end of any line except the last one and press enter. You get a new list item, as expected.
- Put your cursor at the end of the very last line and press Enter. You don't get a list item until you type something on that line.
I've likewise found that if I have this text in a text view:
Hello World\n\nList here\n
If I format the text List here
as a bulleted list (by creating an NSMutableParagraphStyle
with its textLists
property set and adding it to the attributed string for that range) and press Enter at the end of the word Here
, I get a new bullet item automatically. But if I do the same thing without having that last newline after Here
, the new list item is not inserted.
How can I make the list auto-continuation behavior work at the end of the document?