-
-
Save darkseed/966312 to your computer and use it in GitHub Desktop.
How to use add pinch-to-zoom to a TextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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