问题
使用手势缩放和旋转UITextView后,文本有时会模糊,因为转换应用于栅格化的位图。
提议的解决方案
手势完成后,我需要将变换返回到身份变换,根据比例调整帧大小,根据比例调整字体大小,然后重新应用旋转变换。
实施尝试
// 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)];
备注
我尝试的所有内容几乎都有效。在某些情况下,特别是在多次手势(复合错误)之后,文本不完全正确,并且会导致之前没有的换行符。