自定义表情键盘

自定义键盘:


-(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 allocinitWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 30, [UIScreen mainScreen].bounds.size.width30)];

    [self.view addSubview:bgView];

    

    //表情

    UIButton *emojiBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    emojiBtn.frame = CGRectMake(3052020);

    [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 -- 表情collectionViewitem个数,每页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 - 1removeFromSuperview];

    }

    UILabel *label = [[UILabel allocinitWithFrame:CGRectMake(003030)];

    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.width50);

        setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 40, [UIScreen mainScreen].bounds.size.width40);

    }];

    

}


#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.width50);

    }];

    //获取键盘的高度

    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.width40);

    }];

}


#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.width50);

        setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 40, [UIScreen mainScreen].bounds.size.width40);

    }];

}

效果图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值