简体   繁体   中英

Scaling UITextView And Font Size Change

When I add UIPinchGestureRecognizer to my UITextView:

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(didScaleView:)];
[pinchRecognizer setDelegate:self];
[textView addGestureRecognizer:pinchRecognizer];

the view increases in size when scaling it up and all text inside becomes increased also. However, when I print UIFont or attributedText properties:

NSLog(@"in didScaleView font %@",view.font);

the font size remains always the same (for example, 22px). Here is the scaling part:

-(void)didScaleView:(id)sender{

[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    lastScale = 1.0;
    return;
}

CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];

}

So, what controls the font size change (ie how does it change) and how to keep it unchanged when scaling the UITextView?

The Affine Transform scaling factor is applied to text like anything else. So the onscreen text size should be the nominal textsize times the scaling factor. Ie, a 22px typeface zoomed by pinching to 1.25 scale would be 22 * 1.25 = 27.5px typeface.

Therefore, to keep the text size unchanged, you'd have to do the reverse: In your pinch recognizer, redraw the text in a typesize that is the nominal size DIVIDED by the scaling factor. Hence, draw 22px type at 22/1.25 = (about) 18px fontsize - which when scaled would be close to your original 22px size.

Clearly rounding is a potential issue, and you need to ensure the views of the text objects actually get redrawn. Does this help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM