Search code examples
iosuitextviewclip

UITextView is clipped after applying a transform and setting new typing attributes


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.

enter image description here

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?


Solution

  • In the end I fixed this OS bug with wrapping the text view in another UIView and apply the transform to the UIView instead of the textview inside.