-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUITTTAttributedLabel.m
executable file
·106 lines (93 loc) · 3.2 KB
/
UITTTAttributedLabel.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
//
// UITTTAttributedLabel.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-8.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "UITTTAttributedLabel.h"
@interface UITTTAttributedLabel ()
@property (nonatomic, assign) BOOL isSelectedForMenu;
@property (nonatomic, copy) UITTTLabelTapBlock tapBlock;
@property (nonatomic, copy) UITTTLabelTapBlock deleteBlock;
@property (nonatomic, copy) UIColor *copyingColor;
@end
@implementation UITTTAttributedLabel
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_deleteBlock = nil;
self.copyingColor = [UIColor colorWithHexString:@"0xc0c1c2"];
// self.leading = 0.0;//行间距,像素值
}
return self;
}
#pragma mark Tap
-(void)addTapBlock:(void(^)(id aObj))block{
_tapBlock = block;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tap];
}
-(void)addDeleteBlock:(UITTTLabelTapBlock)block{
_deleteBlock = block;
}
-(void)handleTap:(UIGestureRecognizer*) recognizer{
if (_tapBlock) {
_tapBlock(self);
}
}
#pragma mark LongPress
-(void)addLongPressForCopy{
_isSelectedForMenu = NO;
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePress:)];
[self addGestureRecognizer:press];
}
-(void)addLongPressForCopyWithBGColor:(UIColor *)color{
self.copyingColor = color;
[self addLongPressForCopy];
}
-(void)handlePress:(UIGestureRecognizer*) recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
if (!_isSelectedForMenu) {
_isSelectedForMenu = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(menuControllerWillHide:)
name:UIMenuControllerWillHideMenuNotification
object:nil];
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
self.backgroundColor = self.copyingColor;
}
}
}
- (void)menuControllerWillHide:(NSNotification*)notification{
if (_isSelectedForMenu) {
_isSelectedForMenu = NO;
self.backgroundColor = [UIColor clearColor];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillHideMenuNotification object:nil];
}
}
//UIMenuController
- (BOOL)canPerformAction:(SEL)action
withSender:(__unused id)sender
{
BOOL canPerformAction = NO;
if (action == @selector(copy:)) {
canPerformAction = YES;
}else if (action == @selector(delete:) && _deleteBlock){
canPerformAction = YES;
}
return canPerformAction;
}
#pragma mark - UIResponderStandardEditActions
- (void)copy:(__unused id)sender {
[[UIPasteboard generalPasteboard] setString:self.text];
}
- (void)delete:(__unused id)sender {
if (_deleteBlock) {
_deleteBlock(self);
}
}
@end