当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS表情键盘的简单实现代码

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

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;
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网