I have a UITextView that can be scaled and rotated. Its text is edited by the user. Text attributes can be changed too e.g. text and background color, font, etc.
Now the funny thing, if I apply a scale transform which shrinks the view (e.g. 0.5), then its text is clipped. Behaviour is the same if I set view.transform or view.layer.transform. It is caused by the internal UITextContainerView which looks like it is scaled twice e.g. if the text view is scaled 0.5x then the UITextContainerView has half size of the shrunken text view. The frame and bounds of the UITextContainerView have the same value and it is the size of the shrunk text view, which looks like it is correct. UITextContainerView transform and its layer's transform are identity.

Manually changing the frame and bounds of the UITextContainerView is automatically reset thus does not work. Also clipsToBounds and masksToBounds do not fix the clipping.
This is happening on iOS 11.4 in simulator and in real devices. If I remove just the setting of new typing attributes, then the UITextContainerView behaves correctly and the text is not clipped.
This is casing issues, if I remove it then the text is not clipped anymore:
textView.typingAttributes = updatedTypingAttributes
Anyone hit this bug? How can I solve this?
Solved it by overriding transform on my UITextView subclass. No need for a parent UIView.
override var transform: CGAffineTransform {
didSet {
guard transform != oldValue else { return }
// alternatively, if transform == .identity
if transform.a == 1 {
setNeedsLayout()
layoutIfNeeded()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With