iOS系統(tǒng)表情鍵盤

UITextField和UITextView的inputView屬性就是用來加入自定義鍵盤的,使用時(shí)賦值即可:

myTF.inputView = _emojiView;

恢復(fù)默認(rèn)鍵盤:

myTF.inputView = nil;

有了這個(gè)屬性我們只需要寫一個(gè)自定義的鍵盤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] 獲取系統(tǒng)emoji表情,用collectionView展示出來,展示帶有emoji表情的字符串時(shí)要先轉(zhuǎn)碼

[contentStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

.m

#import "CustomEmojiView.h"

//將數(shù)字轉(zhuǎn)為
#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;
    //每個(gè)分區(qū)的左右邊距
    CGFloat sectionOffset = (kScreenWidth - 8 * 30 - 7 * 15) / 2;
    //分區(qū)內(nèi)容偏移
    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:@"發(fā)送" 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];
    }
}

//發(fā)送表情
- (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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,171評(píng)論 3 119
  • 鳳棲南路一標(biāo)路燈基礎(chǔ)開挖 鳳棲南路二標(biāo)護(hù)腳墻施工 田園路排水溝開挖
    二七區(qū)交通局閱讀 163評(píng)論 0 0
  • 3月30日 天氣晴 周五 又到周末了,今天晚上因?yàn)樗⒀赖膯栴}我們娘倆又起了戰(zhàn)爭,每次晚上的洗漱閨女都...
    李雨桐媽媽閱讀 371評(píng)論 0 2
  • 姓名:易平香 企業(yè)名稱:東莞耀升機(jī)電有限公司 組別:AT感謝組/272期努力一組 【日精進(jìn)打卡第155天】 【知~...
    shine1yi閱讀 149評(píng)論 0 0
  • 再過三天就是元宵了,越來越多的人開始回到城市,大家相互說著過年趣事 卻在熱鬧中漸漸發(fā)現(xiàn),好像,年味更淡了。 什么是...
    不列顛的貓閱讀 426評(píng)論 0 0

友情鏈接更多精彩內(nèi)容