单军华
2018-07-12 3e8437ae559487362fae3525beb79c534c213a51
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
//  HMEmoticonTextView.m
//  表情键盘
//
//  Created by 刘凡 on 16/3/3.
//  Copyright © 2016年 itcast. All rights reserved.
//
 
#import "HMEmoticonTextView.h"
#import "HMEmoticonAttachment.h"
#import "HMEmoticonInputView.h"
 
@implementation HMEmoticonTextView {
    UILabel *_placeHolderLabel;
    UILabel *_lengthTipLabel;
    NSMutableArray <NSLayoutConstraint *> *_lengthTipLabelCons;
    
    /// 表情输入视图
    HMEmoticonInputView *_emoticonInputView;
}
 
#pragma mark - 设置属性
- (BOOL)isUseEmoticonInputView {
    return self.inputView != nil;
}
 
- (void)setUseEmoticonInputView:(BOOL)useEmoticonInputView {
    
    if (self.isUseEmoticonInputView == useEmoticonInputView) {
        return;
    }
    
    self.inputView = (self.inputView == nil) ? _emoticonInputView : nil;
    [self reloadInputViews];
}
 
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder.copy;
    _placeHolderLabel.text = _placeholder;
}
 
- (void)setMaxInputLength:(NSInteger)maxInputLength {
    _maxInputLength = maxInputLength;
    
    [self textChanged];
}
 
- (void)setFont:(UIFont *)font {
    _placeHolderLabel.font = font;
    
    [super setFont:font];
}
 
- (void)setText:(NSString *)text {
    [super setText:text];
    
    [self textChanged];
}
 
- (UIColor *)textColor {
    UIColor *color = [super textColor];
    
    return (color == nil) ? [UIColor darkGrayColor] : color;
}
 
- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    
    [self textChanged];
}
 
#pragma mark - 构造函数
- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
    self = [super initWithFrame:frame textContainer:textContainer];
    
    if (self) {
        self.textColor = [UIColor darkGrayColor];
        self.font = [UIFont systemFontOfSize:18];
        
        [self prepareUI];
    }
    
    return self;
}
 
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    
    if (self) {
        [self prepareUI];
    }
    
    return self;
}
 
- (void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];
    
    [self layoutIfNeeded];
}
 
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
#pragma mark - 公共函数
- (NSString *)emoticonText {
    
    NSAttributedString *attributeText = self.attributedText;
    NSMutableString *stringM = [NSMutableString string];
    
    [attributeText
     enumerateAttributesInRange:
     NSMakeRange(0, attributeText.length)
     options:0
     usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
         
         HMEmoticonAttachment *attachment = attrs[@"NSAttachment"];
         if (attachment != nil) {
             [stringM appendString:attachment.text];
         } else {
             [stringM appendString:[attributeText.string substringWithRange:range]];
         }
     }];
    
    return stringM.copy;
}
 
- (void)insertEmoticon:(HMEmoticon *)emoticon isRemoved:(BOOL)isRemoved {
    
    if (isRemoved) {
        [self deleteBackward];
        
        return;
    }
    
    if (emoticon.isEmoji) {
        [self replaceRange:[self selectedTextRange] withText:emoticon.emoji];
        
        return;
    }
    
    NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    
    NSAttributedString *emoticonStr = [HMEmoticonAttachment emoticonStringWithEmoticon:emoticon font:self.font textColor:self.textColor];
    
    NSRange range = self.selectedRange;
    [attributeText replaceCharactersInRange:range withAttributedString:emoticonStr];
    
    self.attributedText = attributeText;
    self.selectedRange = NSMakeRange(range.location + 1, 0);
    
    [self.delegate textViewDidChange:self];
    [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:self];
}
 
- (void)updateTipLabelBottomConstraints:(UIView *)view {
    
    // 判断 view 是否是当前的子视图
    if (![self.subviews containsObject:view]) {
        NSLog(@"当前仅支持相对 textView 子视图的控件参照");
        return;
    }
    
    [self removeConstraints:_lengthTipLabelCons];
    
    [_lengthTipLabelCons removeAllObjects];
    CGFloat margin = 5;
    [_lengthTipLabelCons addObject:[NSLayoutConstraint
                                    constraintWithItem:_lengthTipLabel
                                    attribute:NSLayoutAttributeTrailing
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:self
                                    attribute:NSLayoutAttributeTrailing
                                    multiplier:1.0
                                    constant:-margin]];
    [_lengthTipLabelCons addObject:[NSLayoutConstraint
                                    constraintWithItem:_lengthTipLabel
                                    attribute:NSLayoutAttributeBottom
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:view
                                    attribute:NSLayoutAttributeTop
                                    multiplier:1.0
                                    constant:-margin]];
    
    [self addConstraints:_lengthTipLabelCons];
}
 
#pragma mark - 监听方法
- (void)textChanged {
    _placeHolderLabel.hidden = self.hasText;
    
    if (_maxInputLength <= 0) {
        _lengthTipLabel.hidden = YES;
        
        return;
    }
    _lengthTipLabel.hidden = NO;
    
    NSInteger len = _maxInputLength - self.emoticonText.length;
    _lengthTipLabel.text = [NSString stringWithFormat:@"%zd", len];
    _lengthTipLabel.textColor = (len >= 0) ? [UIColor lightGrayColor] : [UIColor redColor];
}
 
#pragma mark - 设置界面
- (void)prepareInputView {
    __weak typeof(self) weakSelf = self;
    _emoticonInputView = [[HMEmoticonInputView alloc] initWithSelectedEmoticon:^(HMEmoticon * _Nullable emoticon, BOOL isRemoved) {
        [weakSelf insertEmoticon:emoticon isRemoved:isRemoved];
    }];
}
 
- (void)prepareUI {
    // 0. 准备输入视图
    [self prepareInputView];
    
    // 1. 注册文本变化通知
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(textChanged)
     name:UITextViewTextDidChangeNotification object:nil];
    
    // 2. 默认属性
    self.alwaysBounceVertical = YES;
    self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    // 3. 尺寸视图
    UIView *sizeView = [[UIView alloc] init];
    sizeView.backgroundColor = [UIColor clearColor];
    sizeView.userInteractionEnabled = NO;
    
    [self insertSubview:sizeView atIndex:0];
    
    sizeView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:[NSLayoutConstraint
                          constraintsWithVisualFormat:@"H:|-0-[sizeView]-0-|"
                          options:0
                          metrics:nil
                          views:@{@"sizeView": sizeView}]];
    [self addConstraints:[NSLayoutConstraint
                          constraintsWithVisualFormat:@"V:|-0-[sizeView]-0-|"
                          options:0
                          metrics:nil
                          views:@{@"sizeView": sizeView}]];
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:sizeView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1.0
                         constant:0]];
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:sizeView
                         attribute:NSLayoutAttributeHeight
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeHeight
                         multiplier:1.0
                         constant:0]];
    
    // 4. 占位标签
    _placeHolderLabel = [[UILabel alloc] init];
    _placeHolderLabel.text = _placeholder;
    _placeHolderLabel.textColor = [UIColor lightGrayColor];
    _placeHolderLabel.font = self.font;
    _placeHolderLabel.numberOfLines = 0;
    
    [self addSubview:_placeHolderLabel];
    
    _placeHolderLabel.translatesAutoresizingMaskIntoConstraints = NO;
    CGFloat leftOffset = 5;
    CGFloat topOffset = 8;
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_placeHolderLabel
                         attribute:NSLayoutAttributeLeading
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeLeading
                         multiplier:1.0
                         constant:leftOffset]];
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_placeHolderLabel
                         attribute:NSLayoutAttributeTop
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeTop
                         multiplier:1.0
                         constant:topOffset]];
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_placeHolderLabel
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationLessThanOrEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1.0
                         constant:-2 * leftOffset]];
    
    // 5. 长度提示标签
    _lengthTipLabel = [[UILabel alloc] init];
    _lengthTipLabel.text = [NSString stringWithFormat:@"%zd", _maxInputLength];
    _lengthTipLabel.textColor = [UIColor lightGrayColor];
    _lengthTipLabel.font = [UIFont systemFontOfSize:12];
    
    [self addSubview:_lengthTipLabel];
    
    _lengthTipLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _lengthTipLabelCons = [NSMutableArray array];
    [_lengthTipLabelCons addObject:[NSLayoutConstraint
                                    constraintWithItem:_lengthTipLabel
                                    attribute:NSLayoutAttributeTrailing
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:self
                                    attribute:NSLayoutAttributeTrailing
                                    multiplier:1.0
                                    constant:-leftOffset]];
    [_lengthTipLabelCons addObject:[NSLayoutConstraint
                                    constraintWithItem:_lengthTipLabel
                                    attribute:NSLayoutAttributeBottom
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:self
                                    attribute:NSLayoutAttributeBottom
                                    multiplier:1.0
                                    constant:-leftOffset]];
    
    [self addConstraints:_lengthTipLabelCons];
}
 
@end