-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHTCopyableLabel.m
130 lines (107 loc) · 3.28 KB
/
HTCopyableLabel.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// HTCopyableLabel.m
// HotelTonight
//
// Created by Jonathan Sibley on 2/6/13.
// Copyright (c) 2013 Hotel Tonight. All rights reserved.
//
#import "HTCopyableLabel.h"
@interface HTCopyableLabel ()
@end
@implementation HTCopyableLabel
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self setup];
}
return self;
}
- (void)awakeFromNib
{
[self setup];
}
- (void)setup
{
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self addGestureRecognizer:_longPressGestureRecognizer];
_copyMenuArrowDirection = UIMenuControllerArrowDefault;
_copyingEnabled = YES;
self.userInteractionEnabled = YES;
}
#pragma mark - Public
- (void)setCopyingEnabled:(BOOL)copyingEnabled
{
if (_copyingEnabled != copyingEnabled)
{
[self willChangeValueForKey:@"copyingEnabled"];
_copyingEnabled = copyingEnabled;
[self didChangeValueForKey:@"copyingEnabled"];
self.userInteractionEnabled = copyingEnabled;
self.longPressGestureRecognizer.enabled = copyingEnabled;
}
}
#pragma mark - Callbacks
- (void)longPressGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.longPressGestureRecognizer)
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
// NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
[self becomeFirstResponder]; // must be called even when NS_BLOCK_ASSERTIONS=0
UIMenuController *copyMenu = [UIMenuController sharedMenuController];
if ([self.copyableLabelDelegate respondsToSelector:@selector(copyMenuTargetRectInCopyableLabelCoordinates:)])
{
[copyMenu setTargetRect:[self.copyableLabelDelegate copyMenuTargetRectInCopyableLabelCoordinates:self] inView:self];
}
else
{
[copyMenu setTargetRect:self.bounds inView:self];
}
copyMenu.arrowDirection = self.copyMenuArrowDirection;
[copyMenu setMenuVisible:YES animated:YES];
}
}
}
#pragma mark - UIResponder
- (BOOL)canBecomeFirstResponder
{
return self.copyingEnabled;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL retValue = NO;
if (action == @selector(copy:))
{
if (self.copyingEnabled)
{
retValue = YES;
}
}
else
{
// Pass the canPerformAction:withSender: message to the superclass
// and possibly up the responder chain.
retValue = [super canPerformAction:action withSender:sender];
}
return retValue;
}
- (void)copy:(id)sender
{
if (self.copyingEnabled)
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *stringToCopy;
if ([self.copyableLabelDelegate respondsToSelector:@selector(stringToCopyForCopyableLabel:)])
{
stringToCopy = [self.copyableLabelDelegate stringToCopyForCopyableLabel:self];
}
else
{
stringToCopy = self.text;
}
[pasteboard setString:stringToCopy];
}
}
@end