美文网首页表情
iOS应用中增加emoji表情输入功能

iOS应用中增加emoji表情输入功能

作者: 风继续吹0 | 来源:发表于2016-08-25 17:01 被阅读82次

    Emoji的介绍参见wiki,iOS支持的emoji表情列表参见
    http://pukupi.com/post/1964/,iOS支持的emoji表情的Unicode编码范围为[0xE001,0xE05A]&[0xE101,0xE15A]&[0xE201,0xE253]&[0xE401&0xE44C]&[0xE501,0xE537],Unicode编码转为NSString的方法为[NSStringstringWithFormat:@"%C", i].使用如下代码可以弹出一个可以选择Emoji表情的输入框,

    - (void)didSelectAFace:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSString *emojiStr = [NSString stringWithFormat:@"%C", button.tag];
    textView.text = [NSString stringWithFormat:@"%@%@",textView.text,emojiStr];
    }
    
    - (void)hideEmojiView{
    [emojiView removeFromSuperview];
    [emojiCloseButton removeFromSuperview];
    }
    
    
    
    - (void)showEmojiView{
    CGFloat x = (self.view.frame.size.width-300.0f)/2;
    CGFloat y = self.view.frame.size.height - 190;
    if (emojiView == nil) {
    self.emojiView = [[UIView alloc] initWithFrame:CGRectMake(x,y, 300.0f, 140.0f)];
    emojiView.backgroundColor = [UIColor lightGrayColor];
    emojiView.alpha = 0.8;
    emojiView.layer.cornerRadius = 6;
    [emojiView.layer setMasksToBounds:YES];
    
    UIScrollView *emojiScrollView = [[UIScrollView alloc] init];
    emojiScrollView.frame = CGRectMake(0, 0 ,300.0f, 140.0f);
    [emojiView addSubview:emojiScrollView];
    int xIndex = 0;
    int yIndex = 0;
    int emojiRangeArray[10] = {0xE001,0xE05A,0xE101,0xE15A,0xE201,0xE253,0xE401,0xE44C,0xE501,0xE537};
    for (int j = 0 ; j<10 ; j+=2 ) {
    int startIndex = emojiRangeArray[j];
    int endIndex = emojiRangeArray[j+1];
    for (int i = startIndex ; i<= endIndex ; i++ ) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(6 + xIndex*32, 6 + yIndex*32, 32.0f, 32.0f);
    
    [button setTitle:[NSString stringWithFormat:@"%C", i]forState:UIControlStateNormal];
    
    button.tag = i;
    
    [button addTarget:self action:@selector(didSelectAFace:)forControlEvents:UIControlEventTouchUpInside];
    
    [emojiScrollView addSubview:button];
    
    
    
    xIndex += 1;
    
    if (xIndex == 9) {
    
    xIndex = 0;
    
    yIndex += 1;
    
    }
    
    }
    
    }
    [emojiScrollView setContentSize:CGSizeMake(300.0f, 12 + (yIndex+1)*32)];
    
    
    
    //closeButton
    
    self.emojiCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    emojiCloseButton.frame = CGRectMake(x-10.0f , y-10.0f , 24.0f, 24.0f);
    
    [emojiCloseButton setImage:[UIImage imageNamed:@"dialog_close.png"]forState:UIControlStateNormal];
    
    [emojiCloseButton addTarget:self action:@selector(hideEmojiView)forControlEvents:UIControlEventTouchUpInside];
    
    
    
    }else {
    
    emojiView.frame = CGRectMake(x,y, 300.0f, 140.0f);
    
    emojiCloseButton.frame = CGRectMake(x-10.0f , y-10.0f , 24.0f, 24.0f);
    
    }
    
    
    
    [self.view addSubview:emojiView];
    
    [self.view addSubview:emojiCloseButton];
    
    
    
    }

    相关文章

      网友评论

        本文标题:iOS应用中增加emoji表情输入功能

        本文链接:https://www.haomeiwen.com/subject/bvdasttx.html