Search code examples
uitextviewcgaffinetransformcgaffinetransformscale

Scale down UITextView with Transform


I am trying to scale down a UITextView with a custom font in a storyboard with constraints. I can easily animated the bounds and also the font-size. However, since it is a custom font the font-changing becomes quite choppy at larger sizes. I figured this could be done with by changing the transform of the UITextView. However, this does not seem to be working. The text view is self sizing in height, but uses a fixed with constant for width (which animated along with the font size/transform). This go gain control over where the text view line breaks.

I've tried all suggested solutions below without success. Either the view keeps it bounds but lowers in resolution. Or the bounds decrease, but faster than the appeared font size resulting in clipping.

Scaling UITextView using contentScaleFactor property

I've also tried to place the text view in a placeholderview where I also animated the height/width constraint multiplier to make sure it decreases in size.


Solution

  • Solved it by placing my UITextView in a placeholder UIView and setting align center X/Y + Equal width/height constraints to it.

    I also animated to multiplier of the width/height contraints between these views to the inverse of the current transform scale. This to compensate for the scaling of the transform.

    textViewWidthConstraint = textViewWidthConstraint.withMultiplier(1/scale)
    textViewHeightConstraint = textViewHeightConstraint.withMultiplier(1/scale)
    placeholderView.transform = CGAffineTransform(scaleX: scale, y: scale)
    

    where function withMultiplier is a rename of the solution presented in https://stackoverflow.com/a/33003217/511299 for editing constraint multipliers.