-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathUITextField+QMUI.m
122 lines (103 loc) · 5.68 KB
/
UITextField+QMUI.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
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// UITextField+QMUI.m
// qmui
//
// Created by QMUI Team on 2017/3/29.
//
#import "UITextField+QMUI.h"
#import "NSObject+QMUI.h"
#import "QMUICore.h"
#import "UIImage+QMUI.h"
@implementation UITextField (QMUI)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// iOS 13 及以下版本需要重写该方法才能替换
// - (id) _clearButtonImageForState:(unsigned long)arg1;
// https://github.com/Tencent/QMUI_iOS/issues/1477
if (@available(iOS 14.0, *)) {
} else {
OverrideImplementation([UITextField class], NSSelectorFromString(@"_clearButtonImageForState:"), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^UIImage *(UITextField *selfObject, UIControlState firstArgv) {
if (selfObject.qmui_clearButtonImage && (firstArgv & UIControlStateNormal) == UIControlStateNormal) {
return selfObject.qmui_clearButtonImage;
}
// call super
UIImage *(*originSelectorIMP)(id, SEL, UIControlState);
originSelectorIMP = (UIImage *(*)(id, SEL, UIControlState))originalIMPProvider();
UIImage *result = originSelectorIMP(selfObject, originCMD, firstArgv);
return result;
};
});
}
});
}
- (void)setQmui_selectedRange:(NSRange)qmui_selectedRange {
self.selectedTextRange = [self qmui_convertUITextRangeFromNSRange:qmui_selectedRange];
}
- (NSRange)qmui_selectedRange {
return [self qmui_convertNSRangeFromUITextRange:self.selectedTextRange];
}
- (UIButton *)qmui_clearButton {
return [self qmui_valueForKey:@"clearButton"];
}
static char kAssociatedObjectKey_clearButtonImage;
- (void)setQmui_clearButtonImage:(UIImage *)qmui_clearButtonImage {
objc_setAssociatedObject(self, &kAssociatedObjectKey_clearButtonImage, qmui_clearButtonImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (@available(iOS 14.0, *)) {
[self.qmui_clearButton setImage:qmui_clearButtonImage forState:UIControlStateNormal];
// 如果当前 clearButton 正在显示的时候把自定义图片去掉,需要重新 layout 一次才能让系统默认图片显示出来
if (!qmui_clearButtonImage) {
[self setNeedsLayout];
}
}
}
- (UIImage *)qmui_clearButtonImage {
return (UIImage *)objc_getAssociatedObject(self, &kAssociatedObjectKey_clearButtonImage);
}
- (NSRange)qmui_convertNSRangeFromUITextRange:(UITextRange *)textRange {
NSInteger location = [self offsetFromPosition:self.beginningOfDocument toPosition:textRange.start];
NSInteger length = [self offsetFromPosition:textRange.start toPosition:textRange.end];
return NSMakeRange(location, length);
}
- (UITextRange *)qmui_convertUITextRangeFromNSRange:(NSRange)range {
if (range.location == NSNotFound || NSMaxRange(range) > self.text.length) {
return nil;
}
UITextPosition *beginning = self.beginningOfDocument;
UITextPosition *startPosition = [self positionFromPosition:beginning offset:range.location];
UITextPosition *endPosition = [self positionFromPosition:beginning offset:NSMaxRange(range)];
return [self textRangeFromPosition:startPosition toPosition:endPosition];
}
static char kAssociatedObjectKey_respondsToDeleteActionAtLeading;
- (void)setQmui_respondsToDeleteActionAtLeading:(BOOL)respondsToDeleteActionAtLeading {
objc_setAssociatedObject(self, &kAssociatedObjectKey_respondsToDeleteActionAtLeading, @(respondsToDeleteActionAtLeading), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[QMUIHelper executeBlock:^{
OverrideImplementation([UITextField class], @selector(deleteBackward), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UITextField *selfObject) {
BOOL deletingAtLeading = NSEqualRanges(selfObject.qmui_selectedRange, NSMakeRange(0, 0));
if (selfObject.qmui_respondsToDeleteActionAtLeading && deletingAtLeading) {
QMUILog(@"UITextField (QMUI)", @"光标已在输入框开头的情况下依然按下删除按键。");
if ([selfObject.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
[selfObject.delegate textField:selfObject shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
}
}
// call super
void (*originSelectorIMP)(id, SEL);
originSelectorIMP = (void (*)(id, SEL))originalIMPProvider();
originSelectorIMP(selfObject, originCMD);
};
});
} oncePerIdentifier:@"UITextField (QMUI) delete"];
}
- (BOOL)qmui_respondsToDeleteActionAtLeading {
return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_respondsToDeleteActionAtLeading)) boolValue];
}
@end