From 83f879cf84a021fb545efab3a9987a0c2edf0a97 Mon Sep 17 00:00:00 2001 From: Marina Puzyreva Date: Mon, 25 Jul 2016 12:42:00 +0400 Subject: [PATCH] Forked and redesigned --- ISEmojiView/ISEmojiView.h | 63 +-------- ISEmojiView/ISEmojiView.m | 287 ++++++++++++++++---------------------- 2 files changed, 121 insertions(+), 229 deletions(-) diff --git a/ISEmojiView/ISEmojiView.h b/ISEmojiView/ISEmojiView.h index f2db041..bf2e6c2 100644 --- a/ISEmojiView/ISEmojiView.h +++ b/ISEmojiView/ISEmojiView.h @@ -3,83 +3,26 @@ // ISEmojiViewSample // // Created by isaced on 14/12/25. -// Copyright (c) 2014 Year isaced. All rights reserved. +// Copyright (c) 2014年 isaced. All rights reserved. // #import @protocol ISEmojiViewDelegate; -/** - * The custom keyboard view - */ @interface ISEmojiView : UIView -/** - * ISEmojiView Delegate - */ @property (nonatomic, assign) id delegate; -/** - * Emoji container used to store all the elements - */ -@property (nonatomic, strong) UIScrollView *scrollView; - -/** - * UIPageControl for next page - */ -@property (nonatomic, strong) UIPageControl *pageControl; - -/** - * the textField view (eg:TextField,TextView) - */ -@property (nonatomic, strong) UIView *textField; - -/** - * Are animation allowed (default:NO) - * - * require set textField to get point - */ -@property (nonatomic, assign) BOOL popAnimationEnable; - -/** - * init ISEmojiView - */ -- (instancetype)initWithTextField:(UIView *)textField - delegate:(id)delegate; - @end -/** - * ISEmojiView Delegate - * - * Used to respond to some of the operations callback - */ @protocol ISEmojiViewDelegate -/** - * When you choose a Emoji - * - * @param emojiView The emoji keyboard view - * @param emoji The selected emoji character - */ -(void)emojiView:(ISEmojiView *)emojiView didSelectEmoji:(NSString *)emoji; - -/** - * When the touch bottom right corner of the delete key - * - * You should remove the last character(emoji) in the text box - * @param emojiView The emoji keyboard view - * @param deletebutton The delete button - */ -(void)emojiView:(ISEmojiView *)emojiView didPressDeleteButton:(UIButton *)deletebutton; @end +@interface ISDeleteButton : UIButton -/** - * The Delete Button - * - * You do not care about it - */ -@interface ISDeleteButton : UIButton @end \ No newline at end of file +@end \ No newline at end of file diff --git a/ISEmojiView/ISEmojiView.m b/ISEmojiView/ISEmojiView.m index 822fc45..44adc77 100644 --- a/ISEmojiView/ISEmojiView.m +++ b/ISEmojiView/ISEmojiView.m @@ -3,148 +3,153 @@ // ISEmojiViewSample // // Created by isaced on 14/12/25. -// Copyright (c) 2014 Year isaced. All rights reserved. +// Copyright (c) 2014年 isaced. All rights reserved. // #import "ISEmojiView.h" -static const CGFloat EmojiWidth = 53; -static const CGFloat EmojiHeight = 50; +static const CGFloat EmojiWidth = 43; +static const CGFloat EmojiHeight = 43; static const CGFloat EmojiFontSize = 32; @interface ISEmojiView() -/** - * All emoji characters - */ @property (nonatomic, strong) NSArray *emojis; +@property (nonatomic, strong) UIScrollView *scrollView; +@property (nonatomic, strong) UIPageControl *pageControl; @end @implementation ISEmojiView -- (CGRect)defaultFrame { - return CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 216); -} - -- (instancetype)initWithFrame:(CGRect)frame{ +-(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { - [self initUIWithFrame:frame]; - } - return self; -} - --(instancetype)initWithTextField:(UIView *)textField delegate:(id)delegate { - self = [super init]; - if (self) { - self.delegate = delegate; - self.textField = textField; - } - return self; -} - -- (void)initUIWithFrame:(CGRect)frame { - if (CGRectEqualToRect(frame, CGRectZero)) { - frame = [self defaultFrame]; - } - - self.frame = frame; - - // init emojis - NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ISEmojiList" ofType:@"plist"]; - self.emojis = [NSArray arrayWithContentsOfFile:plistPath]; - - // - NSInteger rowNum = (CGRectGetHeight(frame) / EmojiHeight); - NSInteger colNum = (CGRectGetWidth(frame) / EmojiWidth); - NSInteger numOfPage = ceil((float)[self.emojis count] / (float)(rowNum * colNum)); - - if (rowNum == 0 && colNum == 0) { - return; - } - - // init scrollview - self.scrollView = [[UIScrollView alloc] initWithFrame:frame]; - self.scrollView.pagingEnabled = YES; - self.scrollView.showsHorizontalScrollIndicator = NO; - self.scrollView.showsVerticalScrollIndicator = NO; - self.scrollView.delegate = self; - self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(frame) * numOfPage, - CGRectGetHeight(frame)); - [self addSubview:self.scrollView]; - - // add emojis - - NSInteger row = 0; - NSInteger column = 0; - NSInteger page = 0; - - NSInteger emojiPointer = 0; - for (int i = 0; i < [self.emojis count] + numOfPage - 1; i++) { + self.backgroundColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.90 alpha:1.0f]; + // init emojis + NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ISEmojiList" ofType:@"plist"]; + self.emojis = [NSArray arrayWithContentsOfFile:plistPath]; - // Pagination - if (i % (rowNum * colNum) == 0) { - page ++; // Increase the number of pages - row = 0; // the number of lines is 0 - column = 0; // the number of columns is 0 - }else if (i % colNum == 0) { - // NewLine - row += 1; // Increase the number of lines - column = 0; // The number of columns is 0 - } + //init frames + CGRect emojiFrame = CGRectMake(10, 30, frame.size.width - 55, frame.size.height - 30); + CGRect pageControlFrame = CGRectMake(0, 0, frame.size.width, 30); + CGRect backButtonFrame = CGRectMake(frame.size.width - 55, 30, 55, frame.size.height - 30); - CGRect currentRect = CGRectMake(((page-1) * frame.size.width) + (column * EmojiWidth), - row * EmojiHeight, - EmojiWidth, - EmojiHeight); + // + NSInteger rowNum = (CGRectGetHeight(emojiFrame) / EmojiHeight); + NSInteger colNum = (CGRectGetWidth(emojiFrame) / EmojiWidth); + NSInteger numOfPage = ceil((float)[self.emojis count] / (float)(rowNum * colNum)); - if (row == (rowNum - 1) && column == (colNum - 1)) { - // last position of page, add delete button + // init scrollview + self.scrollView = [[UIScrollView alloc] initWithFrame:emojiFrame]; + self.scrollView.pagingEnabled = YES; + self.scrollView.showsHorizontalScrollIndicator = NO; + self.scrollView.showsVerticalScrollIndicator = NO; + self.scrollView.delegate = self; + self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(emojiFrame) * numOfPage, + CGRectGetHeight(emojiFrame)); + [self addSubview:self.scrollView]; + + // add emojis + + NSInteger row = 0; + NSInteger column = 0; + NSInteger page = 0; + + NSInteger emojiPointer = 0; + for (int i = 0; i < [self.emojis count] - 1; i++) { - ISDeleteButton *deleteButton = [ISDeleteButton buttonWithType:UIButtonTypeCustom]; - [deleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; - deleteButton.frame = currentRect; - deleteButton.tintColor = [UIColor blackColor]; - [self.scrollView addSubview:deleteButton]; + // Pagination + if (i % (rowNum * colNum) == 0) { + page ++; // Increase the number of pages + row = 0; // the number of lines is 0 + column = 0; // the number of columns is 0 + }else if (i % colNum == 0) { + // NewLine + row += 1; // Increase the number of lines + column = 0; // The number of columns is 0 + } + + CGRect currentRect = CGRectMake(((page-1) * emojiFrame.size.width) + (column * EmojiWidth), + row * EmojiHeight, + EmojiWidth, + EmojiHeight); - }else{ NSString *emoji = self.emojis[emojiPointer++]; - + // init Emoji Button UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom]; emojiButton.titleLabel.font = [UIFont fontWithName:@"Apple color emoji" size:EmojiFontSize]; [emojiButton setTitle:emoji forState:UIControlStateNormal]; [emojiButton addTarget:self action:@selector(emojiButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; - + emojiButton.frame = currentRect; [self.scrollView addSubview:emojiButton]; + + column++; } - column++; + // add PageControl + self.pageControl = [[UIPageControl alloc] init]; + self.pageControl.hidesForSinglePage = YES; + self.pageControl.currentPage = 0; + self.pageControl.backgroundColor = [UIColor clearColor]; + self.pageControl.numberOfPages = numOfPage; + self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; + self.pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor]; + CGSize pageControlSize = [self.pageControl sizeForNumberOfPages:numOfPage]; + self.pageControl.center = CGPointMake(pageControlFrame.size.width/2.0f, + pageControlFrame.size.height/2.0f + 3); + [self.pageControl addTarget:self action:@selector(pageControlTouched:) forControlEvents:UIControlEventValueChanged]; + [self addSubview:self.pageControl]; + + [self initBackspaceButtonWithFrame:backButtonFrame]; + + [self initBordersWithFrame:frame]; } - - // add PageControl - self.pageControl = [[UIPageControl alloc] init]; - self.pageControl.hidesForSinglePage = YES; - self.pageControl.currentPage = 0; - self.pageControl.backgroundColor = [UIColor clearColor]; - self.pageControl.numberOfPages = numOfPage; - CGSize pageControlSize = [self.pageControl sizeForNumberOfPages:numOfPage]; - self.pageControl.frame = CGRectMake(CGRectGetMidX(frame) - (pageControlSize.width / 2), - CGRectGetHeight(frame) - pageControlSize.height + 5, - pageControlSize.width, - pageControlSize.height); - [self.pageControl addTarget:self action:@selector(pageControlTouched:) forControlEvents:UIControlEventValueChanged]; - [self addSubview:self.pageControl]; - - // default allow animation - self.popAnimationEnable = NO; - - self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; + return self; } -#pragma mark Action +- (void)initBordersWithFrame: (CGRect)frame { + UIView * borderLeft = [[UIView alloc] initWithFrame:CGRectMake(0, + 0, + 6, + frame.size.height)]; + borderLeft.backgroundColor = [UIColor whiteColor]; + [self addSubview:borderLeft]; + UIView * borderRight = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width - 6, + 0, + 6, + frame.size.height)]; + borderRight.backgroundColor = [UIColor whiteColor]; + [self addSubview:borderRight]; + UIView * borderBottom = [[UIView alloc] initWithFrame:CGRectMake(0, + frame.size.height - 6, + frame.size.width, + 6)]; + borderBottom.backgroundColor = [UIColor whiteColor]; + [self addSubview:borderBottom]; +} + +- (void)initBackspaceButtonWithFrame:(CGRect)backSpaceFrame { + + ISDeleteButton *deleteButton = [ISDeleteButton buttonWithType:UIButtonTypeCustom]; + [deleteButton setImage:[UIImage imageNamed:@"backspace"] + forState:UIControlStateNormal]; + + [deleteButton addTarget:self + action:@selector(deleteButtonPressed:) + forControlEvents:UIControlEventTouchUpInside]; + deleteButton.frame = CGRectMake(CGRectGetMaxX(backSpaceFrame) - deleteButton.imageView.image.size.width - 3, + backSpaceFrame.origin.y, + deleteButton.imageView.image.size.width/1.5f, + deleteButton.imageView.image.size.height/1.5f); + deleteButton.tintColor = [UIColor blackColor]; + [self addSubview:deleteButton]; + +} + +#pragma mark - Service methods - (void)pageControlTouched:(UIPageControl *)sender { CGRect bounds = self.scrollView.bounds; @@ -169,36 +174,11 @@ static const CGFloat EmojiFontSize = 32; animation.duration = 0.1; animation.autoreverses = YES; [button.layer addAnimation:animation forKey:nil]; - - if (self.popAnimationEnable) { - // Animation emojibutton - UIButton *animationEmojiButton = [UIButton buttonWithType:UIButtonTypeCustom];; - [animationEmojiButton setTitle: [button titleForState:UIControlStateNormal] forState:UIControlStateNormal]; - animationEmojiButton.titleLabel.font = [UIFont fontWithName:@"Apple color emoji" size:EmojiFontSize]; - - // Conver frame from scrollview to self and add to self - animationEmojiButton.frame = [button.superview convertRect:button.frame toView:self]; - [self addSubview:animationEmojiButton]; - - // get animation traget position from textField view - CGPoint newPoint = [self.textField convertPoint:self.textField.center toView:self]; - [UIView animateWithDuration:0.3 animations:^{ - animationEmojiButton.center = newPoint; - animationEmojiButton.alpha = 0; - } completion:^(BOOL finished) { - if (finished) { - // Callback - if ([self.delegate respondsToSelector:@selector(emojiView:didSelectEmoji:)]) { - [self.delegate emojiView:self didSelectEmoji:button.titleLabel.text]; - } - [animationEmojiButton removeFromSuperview]; - } - }]; - }else{ - // Callback - if ([self.delegate respondsToSelector:@selector(emojiView:didSelectEmoji:)]) { - [self.delegate emojiView:self didSelectEmoji:button.titleLabel.text]; - } + + // Callback + if ([self.delegate respondsToSelector:@selector(emojiView:didSelectEmoji:)]) { + NSString * emojiString = button.titleLabel.text; + [self.delegate emojiView:self didSelectEmoji:emojiString]; } } @@ -220,36 +200,5 @@ static const CGFloat EmojiFontSize = 32; @implementation ISDeleteButton -/** - * Draw the delete key - * - * @param rect Context Rect - */ --(void)drawRect:(CGRect)rect{ - - // Rectangle Drawing - UIBezierPath* rectanglePath = UIBezierPath.bezierPath; - [rectanglePath moveToPoint: CGPointMake(5, 25.05)]; - [rectanglePath addLineToPoint: CGPointMake(20.16, 36)]; - [rectanglePath addLineToPoint: CGPointMake(45.5, 36)]; - [rectanglePath addLineToPoint: CGPointMake(45.5, 13.5)]; - [rectanglePath addLineToPoint: CGPointMake(20.16, 13.5)]; - [rectanglePath addLineToPoint: CGPointMake(5, 25.05)]; - [rectanglePath closePath]; - [self.tintColor setStroke]; - rectanglePath.lineWidth = 1; - [rectanglePath stroke]; - - - // Bezier Drawing - UIBezierPath* bezierPath = UIBezierPath.bezierPath; - [bezierPath moveToPoint: CGPointMake(26.5, 20)]; - [bezierPath addLineToPoint: CGPointMake(36.5, 29.5)]; - [bezierPath moveToPoint: CGPointMake(36.5, 20)]; - [bezierPath addLineToPoint: CGPointMake(26.5, 29.5)]; - [self.tintColor setStroke]; - bezierPath.lineWidth = 1; - [bezierPath stroke]; -} @end