表情包组件

简介: 表情包组件

表情是特殊的串

DYCustomEmojiView.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@protocol CustomEmojiDelegate <NSObject>

@optional

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

- (void)didClickSendEmojiBtn;

@end

@interface DYCustomEmojiView : UIView

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

@end

NS_ASSUME_NONNULL_END

DYCustomEmojiView.m

#import "DYCustomEmojiView.h"

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

@interface DYCustomEmojiView () <UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation DYCustomEmojiView {
    NSArray *emojiArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorFromRGB(0xf5f5f6);
        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 = (SCREEN_WIDTH - 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) collectionViewLayout:layout];
    myCollectionView.backgroundColor = UIColorFromRGB(0xf5f5f6);
    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 *line = [UIView viewWithBackgroundColor:LineGrayColor];
    [line setFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
    [self addSubview:line];
    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 = UIColorFromRGB(0xfa2447);
    [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)];
//    [emojiArray[indexPath.section * 24 + indexPath.row] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    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

使用代码:

导入协议

#import "DYCustomEmojiView.h"
@interface XQBMyAskQuestionViewController ()<CustomEmojiDelegate>
@property (nonatomic, strong) DYCustomEmojiView *customEmojiView;

加载表情包:

-(void)fillFaceEmojiToolbar
{

    self.customEmojiView = [[DYCustomEmojiView alloc] initWithFrame:CGRectMake(0, FULL_HEIGHT, FULL_WIDTH, 140)];
    self.customEmojiView.backgroundColor = UIColor.whiteColor;
    self.customEmojiView.userInteractionEnabled = YES;
    self.customEmojiView.delegate = self;
    self.customEmojiView.hidden = YES;

    [self.view addSubview:self.customEmojiView];

}

实现点击协议:

- (void)didClickEmojiLabel:(NSString *)emojiStr
{
    NSLog(@"emojiStr:%@", emojiStr);
    if(!isCommonUnitEmptyString(emojiStr))
    {
//        self.inputTextView.text = [NSString stringWithFormat:@"%@%@", getNotNilString(self.inputTextView.text),[emojiStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSString *str = getNotNilString([emojiStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
        [self.inputTextView.textStorage insertAttributedString:[[NSAttributedString alloc] initWithString:str] atIndex:self.inputTextView.selectedRange.location];
        self.inputTextView.selectedRange = NSMakeRange(self.inputTextView.selectedRange.location+str.length, 0);
    }
}
目录
相关文章
|
7月前
|
人工智能
怎么用Midjourney制作表情包
怎么用Midjourney制作表情包
162 0
|
5月前
|
Linux 开发者 iOS开发
QT:基于QMediaPlayer制作的视频播放器(最下方有整合包,可直接运行)
QMediaPlayer是Qt多媒体模块中的一个核心类,它提供了播放音频和视频内容的功能。这个类的设计旨在简化跨平台的媒体播放,使得开发者能够在多种操作系统(如Linux、Windows、macOS及移动平台)上轻松集成多媒体播放能力到他们的应用中,而无需关心底层实现细节。以下是关于QMediaPlayer的一些关键点:
533 1
|
5月前
|
JavaScript
vue + element UI【实战】音乐播放器/语音条(内含音频的加载、控制,事件监听,信息获取,手机网页阴影的去除等技巧)
vue + element UI【实战】音乐播放器/语音条(内含音频的加载、控制,事件监听,信息获取,手机网页阴影的去除等技巧)
80 1
|
5月前
|
机器学习/深度学习 人工智能 自然语言处理
AIGC生成表情包
7月更文挑战第3天
|
5月前
|
JavaScript
vue 在线聊天实战范例(含选择发送表情、图片、视频、音频,自定义右键快捷菜单,一键复制,左右聊天气泡)
vue 在线聊天实战范例(含选择发送表情、图片、视频、音频,自定义右键快捷菜单,一键复制,左右聊天气泡)
288 0
|
5月前
|
人工智能
【Midjourney教程】设计麻瓜也能10分钟上架一套表情包
【Midjourney教程】设计麻瓜也能10分钟上架一套表情包
73 0
|
7月前
RiPro主题-子主题huzao-child美化包v4.0带更新,附下载插件
压缩包里包含子主题+下载插件+演示数据
86 0
RiPro主题-子主题huzao-child美化包v4.0带更新,附下载插件
|
7月前
|
存储 安全 数据安全/隐私保护
表情包受保护无法发送怎么破解和制作gif表情包
表情包受保护无法发送怎么破解和制作gif表情包
215 0
|
7月前
|
Python
Python制作gif表情包生成工具,斗图再也不会输啦
Python制作gif表情包生成工具,斗图再也不会输啦
|
存储
使用mediapipe实现ikun表情包制作
使用mediapipe实现ikun表情包制作
906 0