forked from cfr/UITextView-PinchZoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUITextView+PinchZoom.m
75 lines (58 loc) · 2.14 KB
/
UITextView+PinchZoom.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// UITextView+PinchZoom.m
//
// Created by Stan Serebryakov <cfr@gmx.us> on 04.12.12.
//
#import "UITextView+PinchZoom.h"
#import "objc/runtime.h"
static int minFontSizeKey;
static int maxFontSizeKey;
static int zoomEnabledKey;
@implementation UITextView (PinchZoom)
- (void)setMaxFontSize:(CGFloat)maxFontSize
{
objc_setAssociatedObject(self, &maxFontSizeKey, [NSNumber numberWithFloat:maxFontSize],
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)maxFontSize
{
return [objc_getAssociatedObject(self, &maxFontSizeKey) floatValue];
}
- (void)setMinFontSize:(CGFloat)maxFontSize
{
objc_setAssociatedObject(self, &minFontSizeKey, [NSNumber numberWithFloat:maxFontSize],
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)minFontSize
{
return [objc_getAssociatedObject(self, &minFontSizeKey) floatValue];
}
- (void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{
if (!self.isZoomEnabled) return;
CGFloat pointSize = (gestureRecognizer.velocity > 0.0f ? 1.0f : -1.0f) + self.font.pointSize;
pointSize = MAX(MIN(pointSize, self.maxFontSize), self.minFontSize);
self.font = [UIFont fontWithName:self.font.fontName size:pointSize];
}
- (void)setZoomEnabled:(BOOL)zoomEnabled
{
objc_setAssociatedObject(self, &zoomEnabledKey, [NSNumber numberWithBool:zoomEnabled],
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (zoomEnabled) {
for (UIGestureRecognizer *recognizer in self.gestureRecognizers) // initialized already
if ([recognizer isKindOfClass:[UIPinchGestureRecognizer class]]) return;
self.minFontSize = self.minFontSize ?: 8.0f;
self.maxFontSize = self.maxFontSize ?: 42.0f;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(pinchGesture:)];
[self addGestureRecognizer:pinchRecognizer];
#if !__has_feature(objc_arc)
[pinchRecognizer release];
#endif
}
}
- (BOOL)isZoomEnabled
{
return [objc_getAssociatedObject(self, &zoomEnabledKey) boolValue];
}
@end