设置表情键盘视图
2016-03-11 13:15
363 查看
有时候开发的时候可能需要做表情键盘,以前都是网上找的demo很多时候并不能很好的适合自己要做的开发项目,就想着自己写一个表情键盘的视图,代码如下。
NSMutableArray *temp = [[NSMutableArray alloc] init];
for (int i = 0;i<105;i++){
//取出图片的UIImage
UIImage *face = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
NSMutableDictionary *dicFace = [NSMutableDictionary dictionary];
[dicFace setValue:face forKey:[NSString stringWithFormat:@"[/%d]",i]];
[temp addObject:dicFace];
}
self.phraseArray = temp;
//将表情包导入工程之后,可以将表情包这样取出来放到数组里暂时保存起来,在表情键盘呼出时再将需要的表情,便历出来加载到表情键盘上
//键盘视图加载似的方法- (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];
}
NSMutableArray *temp = [[NSMutableArray alloc] init];
for (int i = 0;i<105;i++){
//取出图片的UIImage
UIImage *face = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
NSMutableDictionary *dicFace = [NSMutableDictionary dictionary];
[dicFace setValue:face forKey:[NSString stringWithFormat:@"[/%d]",i]];
[temp addObject:dicFace];
}
self.phraseArray = temp;
//将表情包导入工程之后,可以将表情包这样取出来放到数组里暂时保存起来,在表情键盘呼出时再将需要的表情,便历出来加载到表情键盘上
//键盘视图加载似的方法- (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];
}
相关文章推荐
- online_judge_1521
- Java命令行
- 面试之谈
- Android常用限定符
- 异常处理
- 投影不到电视上
- 获取Asset下文本内容和读取图片
- 线性适应元 - Adaptive Linear Element
- python常用函数
- 怎样取消wps 2010首字母大写
- asp.net笔试题
- 测试用例管理之 TestLink
- java核心技术第1章总结
- 让人蛋疼的KMP算法
- iOS多线程详细使用
- Kinect外包团队(长年承接微软Kinect体感项目外包,有大型Kinect案例)
- Python简史
- PAT (Advanced Level) Practise 1036 Boys vs Girls (25)
- OpenCV3.1 xfeatures2d::SIFT 使用
- oracle10g rac stream 多表复制(本地捕获)