Skip to content

Instantly share code, notes, and snippets.

@aral
Created May 11, 2011 10:36
Show Gist options
  • Save aral/966260 to your computer and use it in GitHub Desktop.
Save aral/966260 to your computer and use it in GitHub Desktop.
How to use add pinch-to-zoom to a TextView
// Create a pinch gesture recognizer instance.
self.pinchGestureRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)] autorelease];
// And add it to your text view.
[self.myTextView addGestureRecognizer:self.pinchGestureRecognizer];
// ...
- (void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{
NSLog(@"*** Pinch: Scale: %f Velocity: %f", gestureRecognizer.scale, gestureRecognizer.velocity);
UIFont *font = self.myTextView.font;
CGFloat pointSize = font.pointSize;
NSString *fontName = font.fontName;
pointSize = ((gestureRecognizer.velocity > 0) ? 1 : -1) * 1 + pointSize;
if (pointSize < 13) pointSize = 13;
if (pointSize > 42) pointSize = 42;
self.myTextView.font = [UIFont fontWithName:fontName size:pointSize];
// Save the new font size in the user defaults.
// (UserDefaults is my own wrapper around NSUserDefaults.)
[[UserDefaults sharedUserDefaults] setFontSize:pointSize];
}
@AD-Paladins
Copy link

AD-Paladins commented Aug 22, 2018

There it is the Swift version for this Gist:

Thanks and best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment