-(void)viewDidLoad {
//增加监听,当键盘出现收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
//增加监听,当键盘改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
int emojiRangeArr[10] = {0xE001, 0xE05A, 0xE101, 0xE15A, 0xE201, 0xE253, 0xE401, 0xE44C, 0xE501, 0xE537}; //表情对应的字符串范围
for (int j = 0; j < 10; j+=2) {
int startIndex = emojiRangeArr[j];
int endIndex = emojiRangeArr[j + 1];
for (int i = startIndex; i <= endIndex; i++) {
//将表情字符串放入数组中
[faceArr addObject:[NSString stringWithFormat:@"%C", (unichar)i]];
}
}
[self layoutBgView];
}
-(void)layoutBgView {
bgView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 30, [UIScreen mainScreen].bounds.size.width, 30)];
[self.view addSubview:bgView];
//表情
UIButton *emojiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
emojiBtn.frame = CGRectMake(30, 5, 20, 20);
[emojiBtn setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
[emojiBtn addTarget:self action:@selector(showEmoji:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:emojiBtn];
}
#pragma mark -- 表情按钮事件(出现表情键盘)
-(void)showEmoji:(UIButton *)button {
//布局自定义的表情视图
[self layoutEmojiView];
//将键盘的inputView设置成自己定义的表情视图
contentTF.inputView = emojiView;
//刷新键盘的inputView
[contentTF reloadInputViews];
/*
如果想让键盘再变成系统键盘,只需要把inputView设置成nil,再刷新一下即可,代码如下:
contentTF.inputView = nil;
[contentTF reloadInputViews];
*/
}
#pragma mark -- 自定义表情键盘
-(void) layoutEmojiView
{
//emojiView上放了一个pageControl和一个collectionView,pagecontrol用来显示分页,collectionView用来显示一个个的表情
emojiView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
//分页控制器
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 170, [UIScreen mainScreen].bounds.size.width, 20)];
pageControl.numberOfPages = (emojiArr.count / 28) + (emojiArr.count % 28 == 0?0:1);
pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
pageControl.pageIndicatorTintColor = [UIColor grayColor];
[emojiView addSubview:pageControl];
//布局collectionView,用来显示表情
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.itemSize = CGSizeMake(30, 30);
float xOffset = ([UIScreen mainScreen].bounds.size.width - 7 * 30 - 10 * 6) / 2;
flowLayout.sectionInset = UIEdgeInsetsMake(10, xOffset, 10, xOffset);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 160) collectionViewLayout:flowLayout];
collectionView.pagingEnabled = YES;
collectionView.tag = 100;
flowLayout.minimumLineSpacing = 10;
flowLayout.minimumInteritemSpacing = 5;
collectionView.showsHorizontalScrollIndicator = NO;
collectionView.dataSource = self;
collectionView.delegate = self;
//注册cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"emoji"];
collectionView.backgroundColor = bgView.backgroundColor;
[emojiView addSubview:collectionView];
}
#pragma mark -- 表情collectionView的item个数,每页28个表情
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if ((faceArr.count / 28) + (faceArr.count % 28 == 0?0:1) != section + 1) {
return 28;
}else {
return faceArr.count - 28 *((faceArr.count / 28)+(faceArr.count % 28 == 0?0:1) - 1);
}
}
#pragma mark -- 返回页数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return (faceArr.count / 28) + (faceArr.count % 28 == 0?0:1);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"emoji" forIndexPath:indexPath];
for (int i = (int)cell.contentView.subviews.count; i>0; i--) {
[cell.contentView.subviews[i - 1] removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
label.font = [UIFont systemFontOfSize:25];
label.text = faceArr[indexPath.row + indexPath.section * 28];
[cell.contentView addSubview:label];
return cell;
}
#pragma mark -- cell的点击事件,点击表情
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = faceArr[indexPath.section * 28 + indexPath.row];
contentTF.text = [NSString stringWithFormat:@"%@%@", contentTF.text, str];//将选中的表情在输入框中显示出来
}
#pragma mark -- 翻页后,对分页控制进行更改
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat contenOffset = scrollView.contentOffset.x;
int apage = contenOffset/scrollView.frame.size.width+((int)contenOffset%(int)scrollView.frame.size.width==0?0:1);
pageControl.currentPage = apage;
}
#pragma mark -- 当键盘出现时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
keyHeight = keyboardRect.size.height;
NSLog(@"键盘的高度:%ld", (long)keyHeight);
//改变输入框的位置
[UIView animateWithDuration:0.1 animations:^{
backLabel.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 44 - 40, [UIScreen mainScreen].bounds.size.width, 50);
setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 40, [UIScreen mainScreen].bounds.size.width, 40);
}];
}
#pragma mark -- 当键盘退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
//改变输入框的位置
[UIView animateWithDuration:0.1 animations:^{
backLabel.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50);
}];
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
keyHeight = keyboardRect.size.height;
NSLog(@"键盘的高度:%ld", (long)keyHeight);
//改变输入框的位置
[UIView animateWithDuration:0.1 animations:^{
setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 40, [UIScreen mainScreen].bounds.size.width, 40);
}];
}
#pragma mark -- 当键盘改变时调用
-(void)keyboardChange:(NSNotification *)noti {
//获取键盘的高度
NSDictionary *userInfo = [noti userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
keyHeight = keyboardRect.size.height;
NSLog(@"键盘的高度:%ld", (long)keyHeight);
//改变输入框的位置
[UIView animateWithDuration:0.1 animations:^{
backLabel.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 44 - 40, [UIScreen mainScreen].bounds.size.width, 50);
setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 40, [UIScreen mainScreen].bounds.size.width, 40);
}];
}
效果图: