We are currently using a standard UITextView and setting the text to be displayed using the text
property.
We have overridden the text
property as below to find out whether the text should be left or right aligned.
override public var text: String! {
didSet {
if !text.isEmpty {
self.textAlignment = text.textAlignment
}
}
}
We now want to introduce additional formatting options (such as bold and italic) and, following this tutorial by Kodeco have set our own implementation of UITextStorage for this. The newly added code can be found below.
However our implementation using the override of text
on UITextView doesn't work anymore. What is the best way of getting the right to left behavior back?
We have tried setting textAlignment
on UITextView to the correct value but this seems to be ignored.
We have additionally looked into the writingDirection
key of NSAttributedString, but we don't want to introduce any new characters into the text that would be copied out when using copy-paste.
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)