自定义表情键盘及输入框的实现

制作需要的文件

  • 表情文件
  • 表情文件名称的plist
  • 表情代码与表情名称转换的plist

实现功能的基本原理

## 表情键盘的自定义##
      表情键盘是一个UIView,在view上添加一个scrollView定义两个属性delegate和datasource。在init方法中添加一个观察者监听datasource。当datasource赋值或修改的时候在scrollView上循环添加表情按钮
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //初始化组件
        //初始化表情界面
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.scrollView.pagingEnabled = YES;
        [self addSubview:self.scrollView];
        [self addObserver:self forKeyPath:@"dataSource" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

在监听方法中画键盘

  //获取需要展示的表情的个数
    NSInteger count = [self.dataSource numberOfFaceItemsInFaceKeyBoarad:self];
    
    //计算所需页数3
    int pageNum=21;
    int rowNum=7;
    int pages = ceil(count/pageNum);
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
    
    for (int i = 0; i < count; i++)
    {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        //确定button位置
        //1.在哪一页
        int section = (int)button.tag / pageNum;
        //2.在哪一页第几个
        int index = button.tag % pageNum;
        //3.在第几行
        int row = index / rowNum;
        //4.在第几列
        int cloumn = index % rowNum;
        
        CGFloat w = self.bounds.size.width;
        CGFloat bw = 25;
        CGFloat blankWidth=(w-7*bw)/8;
        CGFloat blankheight=(self.frame.size.height-3*bw)/4;
        
        CGFloat x = blankWidth+ w * section + (bw+blankWidth) * cloumn;
        CGFloat y = (bw+blankheight) * row+blankheight;
        
        button.frame = CGRectMake(x, y, bw, bw);
        
        [button addTarget:self action:@selector(tapFaceButton:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImage * image = [self.dataSource faceKeyBoard:self faceImageWithIndex:button.tag];
        
        [button setImage:image forState:UIControlStateNormal];
        
        [self.scrollView addSubview:button];
    }

另外在定义两个代理方法如下

@protocol FaceKeyBoardDelegate <NSObject>

@optional
//点击选中的表情的 index
- (void)faceKeyBoard:(ZZFaceKeyBoard *)faceKB didTapFaceItemsAtIndex:(NSInteger)index;
@end

@protocol FaceKeyBoardDataSource <NSObject>

@required
//获取要展示的表情个数
- (NSInteger)numberOfFaceItemsInFaceKeyBoarad:(ZZFaceKeyBoard *)faceKB;

//用户获取当前表情的图片
- (UIImage *)faceKeyBoard:(ZZFaceKeyBoard *)faceKB faceImageWithIndex:(NSInteger)index;

@end

到这里表情键盘就画完了

自定义表情输入框

  • 新建一个UITextView的子类引入前面的表情键盘的代理方法并声明3个属性 键盘faceKB 表情数组faces 转码后的字符串plainString。
  • 初始化表情数组及键盘
 self.font = [UIFont systemFontOfSize:18];
        //获取所有表情
        NSString * path = [[NSBundle mainBundle]pathForResource:@"Emoji" ofType:@"plist"];
        self.faces = [NSArray arrayWithContentsOfFile:path];
        
        
        self.faceKB = [[ZZFaceKeyBoard alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 170)];
        
        self.faceKB.backgroundColor = [UIColor greenColor];
        self.faceKB.delegate = self;
        self.faceKB.dataSource = self;

  • 实现前面定义的键盘的获取表情的代理方法
- (NSInteger)numberOfFaceItemsInFaceKeyBoarad:(ZZFaceKeyBoard *)faceKB
{
    return self.faces.count;
}

- (UIImage *)faceKeyBoard:(ZZFaceKeyBoard *)fk faceImageWithIndex:(NSInteger)index
{
    NSString * name = self.faces[index];
    UIImage * image = [UIImage imageNamed:name];
    return image;
}

-实现点击表情的代理方法
重点来了在这里 使用到了一个富文本 >NSTextAttachment 它的作用就是在一堆字符串里显示图片,而且图片的大小是可以自定义的,但是NSTextAttachment的bounds属性使用起来不方便,而且也没办法快速定位是一张什么样的图片,所以我们需要实现一个 >NSTextAttachment 的子类,在子类中需要重新实现

  • (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
    并声明两个属性 size以及 tag
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex {
    return CGRectMake(0, -4, _emojiSize.width, _emojiSize.height);
}
  • 接下来实现ZZFaceKeyBoard的delegate的代理方法
  • (void)faceKeyBoard:(ZZFaceKeyBoard *)faceKB didTapFaceItemsAtIndex:(NSInteger)index
    获取点击哪个表情
    NSString * name = self.faces[index];

 if ([name isEqualToString:@"faceDelete"]) {
        //系统的删除方法 删除一个长度
        [self deleteBackward];
    }
    else
    {
        //显示表情图片
        self.font=[UIFont systemFontOfSize:17];
        int cha= (int)index /20;
        float height=  self.font.lineHeight;
        
        UIImage * image = [UIImage imageNamed:name];
        ZZEmojiTextAttachment * attachment = [[ZZEmojiTextAttachment alloc]init];
        attachment.image = image;
        attachment.emojiSize=CGSizeMake(height, height);
        if (index == 40) {
            attachment.emojiTag=[NSString stringWithFormat:@"[em:%ld]",(long)index-1];
            
        }
        else
        {
            attachment.emojiTag=[NSString stringWithFormat:@"[em:%ld]",(long)index-cha];
        }
        NSAttributedString * attributedStr = [NSAttributedString attributedStringWithAttachment:attachment];
        
        //在光标位置插入emoji
        [self.textStorage insertAttributedString:attributedStr
                                         atIndex:self.selectedRange.location];
        
        //移动光标位置
        self.selectedRange = NSMakeRange(self.selectedRange.location + 1, self.selectedRange.length);
        
        [self resetTextStyle];
    }

通过index获取到表情名字实例化 >ZZEmojiTextAttachment 然后将你们的定好的协议进行赋值 最后插入到textView 的 >testStorage 中,并移动光标

  • 接下来重写plainString 的get方法
    通过遍历 >textStorage 查找 ZZEmojiTextAttachment 并替换成为tag代码如下
-(NSMutableString *)plainString
{
    
    _plainString = [NSMutableString stringWithString:self.textStorage.string];
    __block NSUInteger base = 0;
    
    [self.textStorage enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.textStorage.length)
                                 options:0
                              usingBlock:^(id value, NSRange range, BOOL *stop) {
                                  if (value && [value isKindOfClass:[ZZEmojiTextAttachment class]]) {
                                      [_plainString replaceCharactersInRange:NSMakeRange(range.location + base, range.length)
                                                                  withString:((ZZEmojiTextAttachment *) value).emojiTag];
                                      base += ((ZZEmojiTextAttachment *) value).emojiTag.length - 1;
                                  }
                              }];
    
    
    
    return _plainString;
}

在label中解析代码并显示表情

通过正则表达式查找plainString 的表情代码 然后使用NSTextAttachment 进行替换 然后给label 的attributedText 进行赋值
代码如下:

 NSString * path = [[NSBundle mainBundle]pathForResource:@"expression" ofType:@"plist"];
    NSDictionary *emojiDict =[[NSDictionary alloc]initWithContentsOfFile:path];

    float height=  _showLabel.font.lineHeight;
  NSMutableAttributedString * mainAttr =[[NSMutableAttributedString alloc]initWithString:str];
    
    //通过正则表达式 判断是否 含有特定字符
    NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"\\[em:[0-9]*\\]"options:NSRegularExpressionAnchorsMatchLines error:nil];
    __block NSUInteger location=0;
    
    
    NSArray *matches=[regex matchesInString:str options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, str.length)];
    
    if (matches.count>0) {
        NSRange range={0,str.length};
        [regex enumerateMatchesInString:str options:NSMatchingWithTransparentBounds range:range usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
            //将特定字符 替换为 图片
            NSRange matchRange=[result range];
            NSString *subStr=[str substringWithRange:matchRange];
            
            NSTextAttachment *attachemnt=[[NSTextAttachment alloc]init];
            attachemnt.bounds=CGRectMake(0, -4, height, height);
            attachemnt.image=[UIImage imageNamed:emojiDict[subStr]];
            NSAttributedString *imageString=[NSAttributedString attributedStringWithAttachment:attachemnt];
            
            NSRange newRange={matchRange.location-location,matchRange.length};
            [mainAttr replaceCharactersInRange:newRange withAttributedString:imageString];
            location=location+matchRange.length-1;
            self.showLabel.attributedText=mainAttr;
            
        }];
        
    }
    else
    {
        self.showLabel.text=str;
    }

大功告成
附:所需文件样式截图

demo地址:https://github.com/dtxzp219/ZZEmojiTextView

表情名称plist.png
解析代码.png

效果图

图片.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,585评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,923评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,314评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,346评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,718评论 3 291
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,828评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,020评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,758评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,486评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,722评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,196评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,546评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,211评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,132评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,916评论 0 200
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,904评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,758评论 2 274

推荐阅读更多精彩内容