Skip to content

Instantly share code, notes, and snippets.

@darkseed
Forked from aral/TextViewPinchToZoom
Created May 11, 2011 11:33
Show Gist options
  • Save darkseed/966312 to your computer and use it in GitHub Desktop.
Save darkseed/966312 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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment