When using a customised UITextContainer, setting text alignment

Right now, we’re using a typical UITextView and changing the text property to display the text.

To determine whether the text should be oriented to the left or right, we overrode the text property as seen below.

override public var text: String! {
     didSet {
         if !text.isEmpty {
                self.textAlignment = text.textAlignment
          }
     }
}

As a result, we have set up our own version of UITextStorage in accordance with this tutorial by Kodeco in order to add more formatting options (such as bold and italic). Below is a link to the recently added code.

However, our version, which used UITextView’s text override, is no longer functional. What is the most effective technique to restore the right-to-left behaviour?

Although we have attempted to set the correct value for textAlignment on the UITextView, this appears to be disregarded.

NSAttributedString’s writingDirection key has also been investigated, but we don’t want to add any new characters to the text that would be copied out if copy-paste were to be used.

let layoutManager = NSLayoutManager()
    
let containerSize = CGSize(width: newTextViewRect.width, height: .greatestFiniteMagnitude)

let container = NSTextContainer(size: containerSize)
container.widthTracksTextView = true

layoutManager.addTextContainer(container)
textStorage.addLayoutManager(layoutManager)

textView = UITextView(frame: newTextViewRect, textContainer: container)

This topic was automatically closed after 166 days. New replies are no longer allowed.