iOS系统表情键盘

UITextField和UITextView的inputView属性就是用来加入自定义键盘的,使用时赋值即可:

myTF.inputView = _emojiView;

恢复默认键盘:

myTF.inputView = nil;

有了这个属性我们只需要写一个自定义的键盘View
.h:

#import <UIKit/UIKit.h>

@protocol CustomEmojiDelegate;

@interface CustomEmojiView : UIView

@property (nonatomic, weak) id<CustomEmojiDelegate> delegate;

@end

@protocol CustomEmojiDelegate <NSObject>

@optional

- (void)didClickEmojiLabel:(NSString *)emojiStr;

- (void)didClickSendEmojiBtn;

@end

// [self defaultEmoticons] 获取系统emoji表情,用collectionView展示出来,展示带有emoji表情的字符串时要先转码

[contentStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

.m

#import "CustomEmojiView.h"

//将数字转为
#define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24);

@interface CustomEmojiView () <UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation CustomEmojiView
{
    NSArray *emojiArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"f5f5f6"];
        emojiArray = [self defaultEmoticons];
        [self createUI];
    }
    return self;
}

- (void)createUI {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(30, 30);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 15;
    //每个分区的左右边距
    CGFloat sectionOffset = (kScreenWidth - 8 * 30 - 7 * 15) / 2;
    //分区内容偏移
    layout.sectionInset = UIEdgeInsetsMake(30, sectionOffset, 30, sectionOffset);
    
    UICollectionView *myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 50) collectionViewLayout:layout];
    myCollectionView.backgroundColor = [UIColor colorWithHexString:@"f5f5f6"];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    myCollectionView.bounces = NO;
    myCollectionView.pagingEnabled = YES;
    myCollectionView.showsVerticalScrollIndicator = NO;
    myCollectionView.showsHorizontalScrollIndicator = NO;
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"emojiCell"];
    [self addSubview:myCollectionView];
    
    UIView *emojiFooter = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 50, self.frame.size.width, 50)];
    emojiFooter.backgroundColor = [UIColor whiteColor];
    [self addSubview:emojiFooter];
    
    UIButton *sendEmojiBtn = [[UIButton alloc]initWithFrame:CGRectMake(emojiFooter.frame.size.width - 70, 0, 70, emojiFooter.frame.size.height)];
    [sendEmojiBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendEmojiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sendEmojiBtn.backgroundColor = [UIColor colorWithHexString:@"fa2447"];
    [emojiFooter addSubview:sendEmojiBtn];
    [sendEmojiBtn addTarget:self action:@selector(sendEmoji) forControlEvents:UIControlEventTouchUpInside];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return (emojiArray.count / 24) + (emojiArray.count % 24 == 0 ? 0 : 1);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (((emojiArray.count / 24) + (emojiArray.count % 24 == 0 ? 0 : 1)) != section + 1) {
        return 24;
    }else {
        return emojiArray.count - 24 * section;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"emojiCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[UICollectionViewCell alloc]init];
    }
    
    [self setCell:cell withIndexPath:indexPath];
    
    return cell;
}

- (void)setCell:(UICollectionViewCell *)cell withIndexPath:(NSIndexPath *)indexPath {
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *emojiLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    emojiLabel.text = emojiArray[indexPath.section * 24 + indexPath.row];
    emojiLabel.font = [UIFont systemFontOfSize:25];
    [cell.contentView addSubview:emojiLabel];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *emojiStr = emojiArray[indexPath.section * 24 + indexPath.row];
    //NSLog(@"表情 %@", emojiStr);
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickEmojiLabel:)]) {
        [self.delegate didClickEmojiLabel:emojiStr];
    }
}

//发送表情
- (void)sendEmoji {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickSendEmojiBtn)]) {
        [self.delegate didClickSendEmojiBtn];
    }
}

//表情包
- (NSArray *)defaultEmoticons {
    NSMutableArray *array = [NSMutableArray new];
    for (int i = 0x1F600; i <= 0x1F64F; i++) {
        if (i < 0x1F641 || i > 0x1F644) {
            int sym = EMOJI_CODE_TO_SYMBOL(i);
            NSString *emoT = [[NSString alloc] initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
            [array addObject:emoT];
        }
    }
    return array;
}

@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,019评论 3 119
  • 凤栖南路一标路灯基础开挖 凤栖南路二标护脚墙施工 田园路排水沟开挖
    二七区交通局阅读 131评论 0 0
  • 3月30日 天气晴 周五 又到周末了,今天晚上因为刷牙的问题我们娘俩又起了战争,每次晚上的洗漱闺女都...
    李雨桐妈妈阅读 355评论 0 2
  • 姓名:易平香 企业名称:东莞耀升机电有限公司 组别:AT感谢组/272期努力一组 【日精进打卡第155天】 【知~...
    shine1yi阅读 132评论 0 0
  • 再过三天就是元宵了,越来越多的人开始回到城市,大家相互说着过年趣事 却在热闹中渐渐发现,好像,年味更淡了。 什么是...
    不列颠的猫阅读 378评论 0 0