iOS表情键盘的简单实现代码

 更新时间:2017年03月25日 15:12:01   作者:Kerwin  
这篇文章主要为大家详细介绍了iOS表情键盘的简单实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近用到了表情键盘就去网上找了下,感觉网上的都是为了更大的需求写的,而我并不需要所以就自己写了个简单的实现。
1.用到的表情字符串是从Emojiplist文件里获取到的;

2.需要添加一个观察者:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 
- (void)keyboardWillShow:(NSNotification *)notification
{
  // 键盘显示\隐藏完毕的frame
  CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  // 动画时间
  CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
   
  // 动画
  [UIView animateWithDuration:duration animations:^{
    commentView.minY = -frame.size.height;
  }];
}

3.创建控件:

  //声明的全局变量:
  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];
   
  //文本输入框
  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 = @"评论";
  [commentWhiteColorView addSubview:commentTextField];
   
  //表情和键盘切换按钮
  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];
   
  //发送按钮
  UIButton *sendButton = [UIButton buttonWithFrame:CGRectMake(emojiAndKeyboardButton.maxX, commentTextField.minY, 50, 40) type:UIButtonTypeCustom title:@"发送" 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];
   
  //表情滚动视图
  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];
   
  //从文件里获取到的表情字符串数组
  emojiArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Emoji" ofType:@"plist"]];
   
  CGFloat emojiButtonWidth = kScreenWidth/8;
   
  int i = 0;
  //页数向上取整
  int page = ceilf(emojiArray.count/32.0);
   
  //UIKit里的页面控制器
  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];
   
  //设置表情滚动视图的contentSize
  emojiScrollView.contentSize = CGSizeMake(kScreenWidth * page, 200);
  //循环创建表情按钮
  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) {
          //如果是第4行第8列就设置删除表情的图片替代字符串,并调用另一个方法
          [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];
         
        //当i等于数组计数时就打断循环
        if (i == emojiArray.count) {
          break;
        }
      }
    }
  }
   
  //手动添加最后一个删除表情按钮
  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];
}
 
//表情按钮事件
- (void)emojiButtonAction:(UIButton *)sender
{
//  NSLog(@"%@",sender.currentTitle);
  NSMutableString *oldText = [NSMutableString stringWithString:commentTextField.text];
  [oldText appendString:sender.currentTitle];
  commentTextField.text = oldText;
}
 
//删除表情按钮事件
- (void)deleteEmojiAction
{
  if (commentTextField.text.length > 1) {
    //判断是否是表情,表情length为2,所以减去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 = @"";
  }
}
 
//在代理方法中调整pageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  if (scrollView == emojiScrollView) {
    pageControl.currentPage = scrollView.contentOffset.x/scrollView.width;
  }
}
 
//表情和键盘切换按钮事件
- (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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS中仿QQ侧滑菜单功能

    iOS中仿QQ侧滑菜单功能

    这篇文章主要介绍了iOS中仿QQ侧滑菜单功能,在实现此功能之前,需要先了解UITabBarController的层级结构,具体实现思路大家可以参考下本文
    2017-07-07
  • IOS CoreLocation实现系统自带定位的方法

    IOS CoreLocation实现系统自带定位的方法

    本篇文章主要介绍了IOS Core Location实现系统自带定位的方法,非常具有实用价值,需要的朋友可以参考下。
    2017-02-02
  • scrollview tableView嵌套解决方案示例

    scrollview tableView嵌套解决方案示例

    这篇文章主要介绍了scrollview tableView嵌套解决方案示例的代码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • iOS实现微信支付流程详解

    iOS实现微信支付流程详解

    本篇文章主要介绍了iOS实现微信支付流程详解 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • iOS开发之清除缓存功能的实现

    iOS开发之清除缓存功能的实现

    现在的绝大多数应用中都存在着清楚缓存的功能,形形色色,各有千秋,所以小编现为大家介绍一种最基础的清除缓存的方法,有需要的可以参考借鉴。下面来一起看看吧。
    2016-09-09
  • iOS 沙盒图片保存读取实例

    iOS 沙盒图片保存读取实例

    下面小编就为大家分享一篇iOS 沙盒图片保存读取实例,具有很的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • 举例讲解Objective-C中@property属性的用法

    举例讲解Objective-C中@property属性的用法

    这篇文章主要介绍了Objective-C中@property属性的用法,包括@property的属性关键字的整理,需要的朋友可以参考下
    2016-03-03
  • iOS中定位当前位置坐标及转换为火星坐标的方法

    iOS中定位当前位置坐标及转换为火星坐标的方法

    这篇文章主要介绍了iOS中获取当前位置坐标及转换为火星坐标的方法,这里的火星坐标指的是我国专门研制的一种加密的坐标系统...需要的朋友可以参考下
    2016-02-02
  • iOS打电话、发短信、发邮件实例代码

    iOS打电话、发短信、发邮件实例代码

    这篇文章主要为大家详细介绍了iOS打电话、发短信、发邮件实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Objective-C中的语法糖示例详解

    Objective-C中的语法糖示例详解

    开发过程中我特别喜欢用语法糖,原因很简单,懒得看到一堆长长的代码,但语法糖简单却不那么简单,下面这篇文章主要给大家介绍了关于Objective-C中语法糖的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2018-01-01

最新评论