iOS表情键盘的简单实现代码
最近用到了表情键盘就去网上找了下,感觉网上的都是为了更大的需求写的,而我并不需要所以就自己写了个简单的实现。
1.用到的表情字符串是从Emojiplist文件里获取到的;
2.需要添加一个观察者:
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
-(void)keyboardWillShow:(NSNotification*)notification
{
//键盘显示\隐藏完毕的frame
CGRectframe=[notification.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
//动画时间
CGFloatduration=[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
//动画
[UIViewanimateWithDuration:durationanimations:^{
commentView.minY=-frame.size.height;
}];
}
3.创建控件:
//声明的全局变量:
UIButton*commentView;
UIView*commentWhiteColorView;
UITextField*commentTextField;
UIButton*emojiAndKeyboardButton;
-(void)initCommentToolbarView
{
commentView=[[UIButtonalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight+230)];
commentView.hidden=YES;
[commentViewaddTarget:selfaction:@selector(commentViewAction)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:commentView];
commentWhiteColorView=[UIViewviewWithFrame:CGRectMake(0,kScreenHeight-50,kScreenWidth,50)backgroundColor:[UIColorwhiteColor]];
commentWhiteColorView.backgroundColor=[UIColorwhiteColor];
[commentViewaddSubview:commentWhiteColorView];
UIView*lightGrayLineView=[UIViewviewWithFrame:CGRectMake(0,0,kScreenWidth,1)backgroundColor:RGB(240,240,240)];
[commentWhiteColorViewaddSubview:lightGrayLineView];
//文本输入框
commentTextField=[[UITextFieldalloc]initWithFrame:CGRectMake(10,5,kScreenWidth-(10+42+60),40)];
commentTextField.font=FONT(14);
commentTextField.leftView=[[UIViewalloc]initWithFrame:CGRectMake(0,0,10,40)];
commentTextField.leftViewMode=UITextFieldViewModeAlways;
commentTextField.backgroundColor=RGB(234,234,234);
commentTextField.placeholder=@"评论";
[commentWhiteColorViewaddSubview:commentTextField];
//表情和键盘切换按钮
emojiAndKeyboardButton=[UIButtonbuttonWithType:UIButtonTypeCustom];
emojiAndKeyboardButton.frame=CGRectMake(commentTextField.maxX+7,0,35,50);
[emojiAndKeyboardButtonsetImage:[UIImageimageNamed:@"icon_emoji_input"]forState:UIControlStateNormal];
[emojiAndKeyboardButtonsetImage:[UIImageimageNamed:@"icon_keyboard_input"]forState:UIControlStateSelected];
[emojiAndKeyboardButtonaddTarget:selfaction:@selector(emojiAndKeyboardButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[commentWhiteColorViewaddSubview:emojiAndKeyboardButton];
//发送按钮
UIButton*sendButton=[UIButtonbuttonWithFrame:CGRectMake(emojiAndKeyboardButton.maxX,commentTextField.minY,50,40)type:UIButtonTypeCustomtitle:@"发送"titleColor:RGB(135,135,135)imageName:nilaction:@selector(sendButtonAction)target:self];
sendButton.titleLabel.font=FONT(14);
[sendButtonsetBorder:1color:RGB(135,135,135)];
[sendButtonsetCornerRadius:3];
[commentWhiteColorViewaddSubview:sendButton];
//表情滚动视图
emojiScrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,commentWhiteColorView.maxY,kScreenWidth,200)];
emojiScrollView.backgroundColor=RGB(244,244,246);
emojiScrollView.delegate=self;
emojiScrollView.pagingEnabled=YES;
[commentViewaddSubview:emojiScrollView];
//从文件里获取到的表情字符串数组
emojiArray=[NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"Emoji"ofType:@"plist"]];
CGFloatemojiButtonWidth=kScreenWidth/8;
inti=0;
//页数向上取整
intpage=ceilf(emojiArray.count/32.0);
//UIKit里的页面控制器
pageControl=[[UIPageControlalloc]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);
[commentViewaddSubview:pageControl];
//设置表情滚动视图的contentSize
emojiScrollView.contentSize=CGSizeMake(kScreenWidth*page,200);
//循环创建表情按钮
for(intcurrentPage=0;currentPage1){
//判断是否是表情,表情length为2,所以减去2
if([emojiArraycontainsObject:[commentTextField.textsubstringWithRange:NSMakeRange(commentTextField.text.length-2,2)]]){
commentTextField.text=[commentTextField.textsubstringToIndex:commentTextField.text.length-2];
}else{
commentTextField.text=[commentTextField.textsubstringToIndex: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){
[commentTextFieldresignFirstResponder];
[UIViewanimateWithDuration:0.5animations:^{
commentView.minY=-230;
}];
}else{
[commentTextFieldbecomeFirstResponder];
}
}
-(void)commentViewAction
{
[commentTextFieldresignFirstResponder];
commentView.hidden=YES;
commentView.minY=0;
commentTextField.text=@"";
emojiAndKeyboardButton.selected=NO;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。