iOS Expression Keyboard Simple Implementation Code

  • 2021-12-04 11:29:21
  • OfStack

Recently, I used the expression keyboard and went to the Internet to find it. I feel that everything on the Internet is written for greater needs, but I don't need it, so I wrote a simple realization myself.
1. The expression string used is obtained from Emojiplist file;

2. Need to add an observer:


[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 
- (void)keyboardWillShow:(NSNotification *)notification
{
  //  Keyboard display \ Hidden frame
  CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  //  Animation time 
  CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
   
  //  Animation 
  [UIView animateWithDuration:duration animations:^{
    commentView.minY = -frame.size.height;
  }];
}

3. Create controls:


  // Declared global variables: 
  UIButton *commentView;
  UIView *commentWhiteColorView;
  UITextField *commentTextField;
  UIButton *emojiAndKeyboardButton;
 
- (void)initCommentToolbarView
{
  commentView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight + 230)];
  commentView.hidden = YES;
  [commentView addTarget:self action:@selector(commentViewAction) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:commentView];
   
  commentWhiteColorView = [UIView viewWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50) backgroundColor:[UIColor whiteColor]];
  commentWhiteColorView.backgroundColor = [UIColor whiteColor];
  [commentView addSubview:commentWhiteColorView];
   
  UIView *lightGrayLineView = [UIView viewWithFrame:CGRectMake(0, 0, kScreenWidth, 1) backgroundColor:RGB(240, 240, 240)];
  [commentWhiteColorView addSubview:lightGrayLineView];
   
  // Text input box 
  commentTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth - (10 + 42 + 60), 40)];
  commentTextField.font = FONT(14);
  commentTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
  commentTextField.leftViewMode = UITextFieldViewModeAlways;
  commentTextField.backgroundColor = RGB(234, 234, 234);
  commentTextField.placeholder = @" Comments ";
  [commentWhiteColorView addSubview:commentTextField];
   
  // Expression and keyboard toggle buttons 
  emojiAndKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
  emojiAndKeyboardButton.frame = CGRectMake(commentTextField.maxX + 7, 0, 35, 50);
  [emojiAndKeyboardButton setImage:[UIImage imageNamed:@"icon_emoji_input"] forState:UIControlStateNormal];
  [emojiAndKeyboardButton setImage:[UIImage imageNamed:@"icon_keyboard_input"] forState:UIControlStateSelected];
  [emojiAndKeyboardButton addTarget:self action:@selector(emojiAndKeyboardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  [commentWhiteColorView addSubview:emojiAndKeyboardButton];
   
  // Send button 
  UIButton *sendButton = [UIButton buttonWithFrame:CGRectMake(emojiAndKeyboardButton.maxX, commentTextField.minY, 50, 40) type:UIButtonTypeCustom title:@" Send " titleColor:RGB(135, 135, 135) imageName:nil action:@selector(sendButtonAction) target:self];
  sendButton.titleLabel.font = FONT(14);
  [sendButton setBorder:1 color:RGB(135, 135, 135)];
  [sendButton setCornerRadius:3];
  [commentWhiteColorView addSubview:sendButton];
   
  // Scroll view of emoticons 
  emojiScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, commentWhiteColorView.maxY, kScreenWidth, 200)];
  emojiScrollView.backgroundColor = RGB(244, 244, 246);
  emojiScrollView.delegate = self;
  emojiScrollView.pagingEnabled = YES;
  [commentView addSubview:emojiScrollView];
   
  // An array of emoticon strings retrieved from a file 
  emojiArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Emoji" ofType:@"plist"]];
   
  CGFloat emojiButtonWidth = kScreenWidth/8;
   
  int i = 0;
  // Pages rounded up 
  int page = ceilf(emojiArray.count/32.0);
   
  //UIKit Page controller in 
  pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, emojiScrollView.maxY, kScreenWidth, 30)];
  pageControl.numberOfPages = page;
  pageControl.backgroundColor = RGB(244, 244, 246);
  pageControl.pageIndicatorTintColor = RGB(206, 206, 206);
  pageControl.currentPageIndicatorTintColor = RGB(121, 121, 121);
  [commentView addSubview:pageControl];
   
  // Set the of the emoticon scrolling view contentSize
  emojiScrollView.contentSize = CGSizeMake(kScreenWidth * page, 200);
  // Loop to create emoticon buttons 
  for (int currentPage = 0; currentPage < page; currentPage++) {
    for (int row = 0; row < 4; row++) {
      for (int column = 0; column < 8; column++) {
        UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom];
        if (row == 3 && column == 7) {
          // If it is the first 4 Line number 8 Column sets the picture replacement string for deleting emoticons, and calls another 1 Methods 
          [emojiButton setImage:[UIImage imageNamed:@"back_icon_input"] forState:UIControlStateNormal];
          [emojiButton addTarget:self action:@selector(deleteEmojiAction) forControlEvents:UIControlEventTouchUpInside];
        }else{
          [emojiButton setTitle:emojiArray[i++] forState:UIControlStateNormal];
          [emojiButton addTarget:self action:@selector(emojiButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        emojiButton.frame = CGRectMake(emojiButtonWidth * column + currentPage * kScreenWidth, 50 * row, emojiButtonWidth, 50);
        [emojiScrollView addSubview:emojiButton];
         
        // When i Break the loop when it is equal to the array count 
        if (i == emojiArray.count) {
          break;
        }
      }
    }
  }
   
  // Manually add the last 1 Delete emoticon buttons 
  UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [emojiButton setImage:[UIImage imageNamed:@"back_icon_input"] forState:UIControlStateNormal];
  emojiButton.frame = CGRectMake(emojiButtonWidth * 7 + 5 * kScreenWidth, 50 * 3, emojiButtonWidth, 50);
  [emojiButton addTarget:self action:@selector(deleteEmojiAction) forControlEvents:UIControlEventTouchUpInside];
  [emojiScrollView addSubview:emojiButton];
}
 
// Emoticon button event 
- (void)emojiButtonAction:(UIButton *)sender
{
//  NSLog(@"%@",sender.currentTitle);
  NSMutableString *oldText = [NSMutableString stringWithString:commentTextField.text];
  [oldText appendString:sender.currentTitle];
  commentTextField.text = oldText;
}
 
// Delete emoticon button event 
- (void)deleteEmojiAction
{
  if (commentTextField.text.length > 1) {
    // Judge whether it is an expression, an expression length For 2 , so subtract 2
    if ([emojiArray containsObject:[commentTextField.text substringWithRange:NSMakeRange(commentTextField.text.length - 2, 2)]]) {
      commentTextField.text = [commentTextField.text substringToIndex:commentTextField.text.length - 2];
    }else{
      commentTextField.text = [commentTextField.text substringToIndex:commentTextField.text.length - 1];
    }
  }else{
    commentTextField.text = @"";
  }
}
 
// Adjust in the proxy method pageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  if (scrollView == emojiScrollView) {
    pageControl.currentPage = scrollView.contentOffset.x/scrollView.width;
  }
}
 
// Emoji and keyboard toggle button events 
- (void)emojiAndKeyboardButtonAction:(UIButton *)sender
{
  sender.selected = !sender.selected;
   
  if (sender.selected == YES) {
    [commentTextField resignFirstResponder];
     
    [UIView animateWithDuration:0.5 animations:^{
      commentView.minY = -230;
    }];
  }else{
    [commentTextField becomeFirstResponder];
  }
}
 
- (void)commentViewAction
{
  [commentTextField resignFirstResponder];
   
  commentView.hidden = YES;
  commentView.minY = 0;
  commentTextField.text = @"";
  emojiAndKeyboardButton.selected = NO;
}


Related articles: