How to access the textContentStorage of UITextView in iOS 16?

Hi TextKit team,

How do I access the textContentStorage of UITextView that is usingTextLayoutManager: true?

I'd like to use the NSTextContentStorageDelegate methods, but unlike NSTextView where it has a property for textContentStorage, it seems UITextView doesn't expose this?

Thanks for your help in advance!

Cheers / Leo

It is a little strange that UITextView doesn't have a convenience property for this. However, you can still access it easily enough via the textLayoutManager's .textContentManager property (NSTextContentStorage being the concrete subclass of NSTextContentManager used by NS/UITextView). And of course you could create an extension to UITextView to add the .textContentStorage property yourself:

extension UITextView {

    var textContentStorage: NSTextContentStorage? {
        return textLayoutManager?.textContentManager as? NSTextContentStorage
    }

}

Hopefully Apple will add this, though, as it would be useful. (I've filed an enhancement request: #FB10376167.)

How to access the textContentStorage of UITextView in iOS 16?
 
 
Q