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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//
// STEmojiCollectionView.m
// STEmojiKeyboard
//
// Created by zhenlintie on 15/5/29.
// Copyright (c) 2015年 sTeven. All rights reserved.
//
#import "STEmojiCollectionView.h"
#define kSTEmojiSize 33
@interface STEmojiView : UIImageView
@property (copy, nonatomic) NSString *emoji;
@end
@implementation STEmojiView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]){
self.userInteractionEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.contentMode = UIViewContentModeCenter;
}
return self;
}
- (void)setEmoji:(NSString *)emoji{
_emoji = emoji;
NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:kSTEmojiSize]};
CGSize size = [emoji sizeWithAttributes:att];
CGFloat scale = [UIScreen mainScreen].scale;
self.frame = CGRectMake(0, 0, size.width, size.height);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGRect rect = CGRectMake(0, 0, size.width*scale, size.height*scale);
UIGraphicsBeginImageContext(rect.size);
[_emoji drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kSTEmojiSize*scale]}];
UIImage *image = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:scale orientation:UIImageOrientationUp];
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
self.image = image;
});
});
}
@end
@interface STEmojiPreview : UIView
- (void)setEmoji:(NSString *)emoji;
@end
@implementation STEmojiPreview{
UILabel *_emojiLabel;
}
- (instancetype)initWithFrame:(CGRect)frame{
if ([super initWithFrame:CGRectMake(0, 0, 102.5, 100)]){
_emojiLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 102.5, 50)];
_emojiLabel.font = [UIFont systemFontOfSize:kSTEmojiSize];
_emojiLabel.textAlignment = NSTextAlignmentCenter;
self.backgroundColor = [UIColor clearColor];
UIImageView *imageBg = [[UIImageView alloc] initWithFrame:self.bounds];
imageBg.image = [UIImage imageNamed:@"keyboard_preview_bg.png"];
[self addSubview:imageBg];
[self addSubview:_emojiLabel];
}
return self;
}
- (void)setEmoji:(NSString *)emoji{
_emojiLabel.text = emoji;
}
@end
#define kSTMinHorizontalPadding 20
#define kSTSectionPadding 10
#define kSTRowCount 5
#define kSTEPTitleHeight 25
@interface STEmojiCollectionView () <UIScrollViewDelegate>
@property (strong, nonatomic) NSMutableArray *sectionViews;
@property (strong, nonatomic) NSMutableArray *sectionTitleViews;
@property (strong, nonatomic) STEmojiPreview *emojiPreview;
@end
@implementation STEmojiCollectionView{
id <UIScrollViewDelegate> _scrollDelegate;
BOOL _beginTouch,_inPreviewing, _inScolling;
CGPoint _currentLocation;
CGFloat _emojiWidth, _emojiHeight;
CGFloat _horizontalPadding;
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]){
[self initialized];
_emojiPreview = [STEmojiPreview new];
[self addSubview:_emojiPreview];
_emojiPreview.hidden = YES;
}
return self;
}
- (void)initialized{
self.showsHorizontalScrollIndicator = NO;
self.multipleTouchEnabled = NO;
self.exclusiveTouch = YES;
self.clipsToBounds = NO;
_inPreviewing = NO;
_inScolling = NO;
[super setDelegate:self];
_sectionViews = [NSMutableArray new];
_sectionTitleViews = [NSMutableArray new];
[self prepareForLayout];
}
// 初始化布局参数
- (void)prepareForLayout{
STEmojiView *tempEmoji = [self makeEmojiView:@"😄"];
_emojiWidth = CGRectGetWidth(tempEmoji.frame);
_emojiHeight = CGRectGetHeight(tempEmoji.frame);
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
int column = 0;
CGFloat w = kSTMinHorizontalPadding;
while (w < screenWidth){
column++;
w += kSTMinHorizontalPadding+_emojiWidth;
}
_horizontalPadding = (screenWidth-column*_emojiWidth)/(column+1);
}
- (void)setDelegate:(id<UIScrollViewDelegate>)delegate{
_scrollDelegate = delegate;
}
#pragma mark - data
- (void)reloadData{
[_sectionViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[_sectionViews removeAllObjects];
[_sectionTitleViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[_sectionTitleViews removeAllObjects];
CGFloat sectionLeft = 0;
for (int section = 0; section < [_emojiDelegate countOfEmojiPageSection]; section++){
UILabel *titleView = [UILabel new];
titleView.textColor = [UIColor colorWithWhite:0.4 alpha:0.5];
titleView.font = [UIFont boldSystemFontOfSize:15];
titleView.text = [_emojiDelegate titleForSection:section];
[titleView sizeToFit];
titleView.frame = CGRectMake(sectionLeft+_horizontalPadding, 0, CGRectGetWidth(titleView.frame), kSTEPTitleHeight);
[_sectionTitleViews addObject:titleView];
[self addSubview:titleView];
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(sectionLeft, 0, 0, CGRectGetHeight(self.bounds))];
// sectionView.layer.borderWidth = 1;
// sectionView.layer.borderColor = [UIColor yellowColor].CGColor;
sectionView.userInteractionEnabled = NO;
[_sectionViews addObject:sectionView];
[self addSubview:sectionView];
NSArray *emojis = [_emojiDelegate emojisForSection:section];
NSInteger row = kSTRowCount;
CGFloat left = 0;
CGFloat top = 0;
for (int i = 0; i < emojis.count; i++){
BOOL nextColumn = (i%row)==0;
if (nextColumn){
left += _horizontalPadding+((i>0)?_emojiWidth:0);
top = 0;
}
STEmojiView *emojiView = [self makeEmojiView:emojis[i]];
[emojiView setFrame:CGRectMake(left, top, _emojiWidth,_emojiHeight)];
top += _emojiHeight;
[sectionView addSubview:emojiView];
}
sectionLeft += left+_emojiWidth+_horizontalPadding;
sectionView.frame = CGRectMake(CGRectGetMinX(sectionView.frame), kSTEPTitleHeight, sectionLeft-CGRectGetMinX(sectionView.frame), CGRectGetHeight(self.bounds)-kSTEPTitleHeight);
// NSLog(@"sectionV:%@",sectionView);
}
[self bringSubviewToFront:_emojiPreview];
self.contentSize = CGSizeMake(sectionLeft, CGRectGetHeight(self.frame));
}
- (void)showSection:(NSInteger)section{
UIView *sectionView = _sectionViews[section];
[self setContentOffset:CGPointMake(CGRectGetMinX(sectionView.frame), 0)];
_inScolling = NO;
}
- (void)showEmojiPreview{
[self getEmojiFromCurrentLocation:^(CGPoint emojiCenterInSelf, NSString *emoji) {
if (emoji){
_emojiPreview.hidden = NO;
[_emojiPreview setEmoji:emoji];
[_emojiPreview setCenter:CGPointMake(emojiCenterInSelf.x,emojiCenterInSelf.y-(CGRectGetHeight(_emojiPreview.frame)-_emojiHeight)/2)];
}
else{
_emojiPreview.hidden = YES;
}
}];
}
- (void)goPreview{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (!_inScolling && _beginTouch){
_inPreviewing = YES;
self.scrollEnabled = NO;
// NSLog(@"已经预览锁定");
[self showEmojiPreview];
}
});
}
- (void)updateTitlePosition{
CGPoint offset = self.contentOffset;
for (int section = 0; section<[_emojiDelegate countOfEmojiPageSection]; section++){
UILabel *titleLabel = _sectionTitleViews[section];
UIView *sectionView = _sectionViews[section];
titleLabel.frame = CGRectMake(MIN(CGRectGetMaxX(sectionView.frame)-CGRectGetWidth(titleLabel.frame)-_horizontalPadding, MAX(CGRectGetMinX(sectionView.frame)+_horizontalPadding, offset.x+_horizontalPadding)), 0, CGRectGetWidth(titleLabel.frame), kSTEPTitleHeight);
}
}
#pragma mark - get
- (STEmojiView *)makeEmojiView:(NSString *)emoji{
STEmojiView *emojiView = [STEmojiView new];
emojiView.emoji = emoji;
return emojiView;
}
- (void)getEmojiFromCurrentLocation:(void(^)(CGPoint emojiCenterInSelf, NSString *emoji))handler{
// NSLog(@"LOCATION: %@",[NSValue valueWithCGPoint:_currentLocation]);
if (_currentLocation.x>0 && _currentLocation.x<self.contentSize.width && _currentLocation.y>kSTEPTitleHeight && _currentLocation.y<(kSTRowCount*_emojiHeight)+kSTEPTitleHeight){
NSInteger section = [self sectionForLocation:_currentLocation];
UIView *sectionView = _sectionViews[section];
[self getEmojiInBounds:sectionView.bounds
location:[self convertPoint:_currentLocation toView:sectionView]
emojis:[_emojiDelegate emojisForSection:section]
handler:^(CGPoint emojiCenter, NSString *emoji) {
if(emoji){
handler([self convertPoint:emojiCenter fromView:sectionView], emoji);
}
else{
handler(CGPointZero, nil);
}
}];
}
else{
handler(CGPointZero, nil);
}
}
- (void)getEmojiInBounds:(CGRect)bounds
location:(CGPoint)location
emojis:(NSArray *)emojis
handler:(void(^)(CGPoint emojiCenter, NSString *emoji))handler{
// NSLog(@"VIEW:%@ %@",[NSValue valueWithCGRect:bounds],[NSValue valueWithCGPoint:location]);
CGFloat w = _horizontalPadding+_emojiWidth;
NSInteger allColumn = (NSInteger)(ceil(emojis.count/(kSTRowCount*1.0)));
NSInteger column = (NSInteger)(MAX(location.x-_horizontalPadding/2, 0))/w + 1;
if (column>allColumn){
column = allColumn;
}
NSInteger row = (NSInteger)(location.y/_emojiHeight) + 1;
NSInteger count = (column-1)*kSTRowCount + row - 1;
// NSLog(@"point: (%ld, %ld)",(long)row,(long)column);
if (count < emojis.count){
CGPoint emojiP = CGPointMake(column*(_emojiWidth+_horizontalPadding)-_emojiWidth/2, row*_emojiHeight-_emojiHeight/2);
// NSLog(@"-- %@",emojis[count]);
handler(emojiP, emojis[count]);
}
else{
handler(CGPointZero, nil);
}
}
- (NSInteger)sectionForLocation:(CGPoint)location{
if (location.x < 0){
return 0;
}
else if (location.x > self.contentSize.width-CGRectGetWidth(self.frame)){
return [_emojiDelegate countOfEmojiPageSection]-1;
}
NSInteger section = 0;
for (;section<_sectionViews.count;section++){
UIView *sectionView = _sectionViews[section];
if (CGRectContainsPoint(sectionView.frame, location)){
return section;
}
}
return NSNotFound;
}
#pragma mark - touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
_beginTouch = YES;
_currentLocation = [[touches anyObject] locationInView:self];
if (!_inScolling && !_inPreviewing){
[self goPreview];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
_currentLocation = [[touches anyObject] locationInView:self];
if (_inPreviewing){
// NSLog(@"滑动--->预览");
[self showEmojiPreview];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchDidEnd];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchDidEnd];
}
- (void)touchDidEnd{
if (!_inScolling){
[self getEmojiFromCurrentLocation:^(CGPoint emojiCenterInSelf, NSString *emoji) {
// NSLog(@"结束点击: %@",emoji);
if (emoji){
[_emojiDelegate emojiDidClicked:emoji];
}
}];
}
_beginTouch = NO;
_inPreviewing = NO;
self.scrollEnabled = YES;
_emojiPreview.hidden = YES;
}
#pragma mark - scroll delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (!_inScolling){
// NSLog(@"开始滑动 无法预览");
}
_inPreviewing = NO;
_inScolling = YES;
[self updateTitlePosition];
if ([_emojiDelegate respondsToSelector:@selector(didScrollToSection:)]){
[_emojiDelegate didScrollToSection:[self sectionForLocation:CGPointMake(scrollView.contentOffset.x, kSTEPTitleHeight+10)]];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (!decelerate){
_inScolling = NO;
// NSLog(@"1结束滑动");
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x>=0 && scrollView.contentOffset.x<=(scrollView.contentSize.width-CGRectGetWidth(scrollView.bounds))){
_inScolling = NO;
// NSLog(@"2结束滑动");
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
_inScolling = NO;
}
@end