Problem
After scaling and rotating a UITextView with gestures, the text is sometimes fuzzy because the transforms are applied to the rasterized bitmap.
Proposed solution
Once the gesture is complete, I need to return the conversion to an identity transformation, resize the frame based on the scale, change the font size based on the scale, and then reapply the rotation transformation.
Attempted Implementation
// Store original center before making changes CGPoint originalCenter = textView.center; // Restore transform to identity (frame property is invalid while transform is not identity) [textView setTransform:CGAffineTransformIdentity]; // Attempt 1 - sizeThatFits ///////////////////////////////////////////////////////////////// // Scale font [textView setFont:[UIFont fontWithName:textView.font.fontName size:textView.font.pointSize*scale]]; // scale refers to the scale of the pinch gesture that just ended // Set frame to sizeThatFits CGSize size = [textView sizeThatFits:maxSize]; // maxSize begins with the view frame size and is scaled at the end of each pinch gesture [textView setFrame:CGRectMake(0, 0, size.width, size.height)]; // Restore Center [textView setCenter:originalCenter]; // Re-apply rotation [textView setTransform:CGAffineTransformMakeRotation(rotation)]; // refers to the total rotation from all rotation gestures // Attempt 2 - manually scale ///////////////////////////////////////////////////////////////// // Scale font [textView setFont:[UIFont fontWithName:textView.font.fontName size:textView.font.pointSize*scale]]; // Scale frame [textView setFrame:CGRectMake(0, 0, textView.frame.size.width*scale, textView.frame.size.height*scale)]; // Restore Center [textView setCenter:originalCenter]; // Re-apply rotation [textView setTransform:CGAffineTransformMakeRotation(rotation)];
Notes
Everything I'm trying almost works. In some cases, especially after several gestures (exacerbates the error), the text does not fit exactly to the right and causes line breaks where they did not exist before.
source share