有时候开发的时候可能需要做表情键盘,以前都是网上找的demo很多时候并不能很好的适合自己要做的开发项目,就想着自己写一个表情键盘的视图,代码如下。
NSMutableArray *temp = [[NSMutableArray alloc] init];
for (int i = 0;i<105;i++){//将表情包导入工程之后,可以将表情包这样取出来放到数组里暂时保存起来,在表情键盘呼出时再将需要的表情,便历出来加载到表情键盘上
//键盘视图加载似的方法- (void)showEmojiView{
int xIndex = 0;
int yIndex = 0;
int emojiRangeArray[12] = {0,10,20,30,40,50,60,70,80,90,100,104};
for (int j = 0 ; j<12 ; j++ ) {
int startIndex = emojiRangeArray[j];
int endIndex = emojiRangeArray[j+1];
for (int i = startIndex ; i<= endIndex ; i++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10 + xIndex*32, 10 + yIndex*32, 32.0f, 32.0f);
NSMutableDictionary *tempdic = [self.phraseArray objectAtIndex:i];
// if (i<10){
// UIImage *tempImage = [tempdic valueForKey:[NSString stringWithFormat:@"</00%d>",i]];
// [button setBackgroundImage:tempImage forState:UIControlStateNormal];
// } else if (i<100){
// UIImage *tempImage = [tempdic valueForKey:[NSString stringWithFormat:@"</0%d>",i]];
// [button setBackgroundImage:tempImage forState:UIControlStateNormal];
// }else
// {
UIImage *tempImage = [tempdic valueForKey:[NSString stringWithFormat:@"[/%d]",i]];
[button setBackgroundImage:tempImage forState:UIControlStateNormal];
// }
button.tag = i;
[button addTarget:self action:@selector(didSelectAFace:)forControlEvents:UIControlEventTouchUpInside];
[_faceScrollView addSubview:button];
xIndex += 1;
if (xIndex == 9) {
xIndex = 0;
yIndex += 1;
}
}
}
[_faceScrollView setContentSize:CGSizeMake(300.0f, 12 + (yIndex+1)*32)];
}
//点击表情时调用的方法
-(void)didSelectAFace:(id)sender
{
UIButton *tempbtn = (UIButton *)sender;
NSMutableDictionary *tempdic = [self.phraseArray objectAtIndex:tempbtn.tag];
NSArray *temparray = [tempdic allKeys];
NSString *faceStr= [NSString stringWithFormat:@"%@",[temparray objectAtIndex:0]];
self.chatViewController.phraseString = faceStr;
[self.chatViewController.messageString appendString:self.chatViewController.phraseString];
[self dismissModalViewControllerAnimated:YES];
}