类似微信表情图文混排(本地自定义表情)

注:仅仅是提供思路 有些问题如隐藏系统自带的selectall按钮 自定义copy按钮 需要再解决

//首先引用 https://github.com/zekunyan/UITextViewDIYEmojiExample 这个第三方 = =里面有demo

 // 这里讲的是如何把<复制图文成纯字符串>方法  添加到点击消息弹出的复制功能上 

- (void)resetTextStyle {

    //After changing text selection, should reset style.

   NSRange wholeRange = NSMakeRange(0, _textView.textStorage.length);


    [_textView.textStorageremoveAttribute:NSFontAttributeNamerange:wholeRange];


    [_textView.textStorageaddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:22.0f]range:wholeRange];

}


- (IBAction)insertEmoji:(UIButton *)sender {

//带[表情]字符串的文本发送 显示成图文混排

    //Create emoji attachment

    EmojiTextAttachment *emojiTextAttachment = [EmojiTextAttachmentnew];


    //Set tag and image

    emojiTextAttachment.emojiTag =_emojiTags[(NSUInteger) sender.tag];

    emojiTextAttachment.image =_emojiImages[(NSUInteger) sender.tag];

   

    //Set emoji size

    emojiTextAttachment.emojiSize =_emojiSizeSlider.value *EMOJI_MAX_SIZE;


 //插入表情

    //Insert emoji image

    [_textView.textStorageinsertAttributedString:[NSAttributedStringattributedStringWithAttachment:emojiTextAttachment]

                                         atIndex:_textView.selectedRange.location];


    //Move selection location

    _textView.selectedRange =NSMakeRange(_textView.selectedRange.location +1, _textView.selectedRange.length);


    //Reset text style

    [selfresetTextStyle];

}


- (IBAction)showPlainText:(id)sender {

//将图文混排转换为带有---[表情]--形式的字符串

    _infoLabel.text = [NSStringstringWithFormat:@"Output: %@", [_textView.textStoragegetPlainString]];

}


//以下是重点= =

-(void)copy:(id)sender

{

    

//重写copy方法    或者自定义:

/********************************************/

以下是自定义textview选中的button  需要放在viewdidload中


 UIMenuItem *menuItem = [[UIMenuItemalloc]initWithTitle:@"复制"action:@selector(mycopy:)];

    UIMenuController *menu = [UIMenuControllersharedMenuController];

    [menu setMenuItems:[NSArrayarrayWithObject:menuItem]];


/********************************************/

//将表情转为字符串存到copy中 下次点击paste 出现的就是--[表情]--这种字符串到输入框 

    UIPasteboard *pasteboard=[UIPasteboardgeneralPasteboard];

    [pasteboardsetString:[NSStringstringWithFormat:@"%@", [_textView.textStoragegetPlainString]]];

    NSLog(@"%@",[[UIPasteboardgeneralPasteboard] string]);

    


}


//如果是自定义 

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

    

//    NSLog(@"%@",action);

    

   if (action == @selector(cut:)){

       return NO;

    }

   else if(action ==@selector(copy:)){

       return NO;

    }

   else if(action ==@selector(paste:)){

       return NO;

    }

   else if(action ==@selector(select:)){

       return NO;

    }

   else if(action ==@selector(selectAll:)){

       return NO;

    }

//需要再自定义的方法 返回yes mycopy方法实现和上面的重写里面的方法一样

   else if(action==@selector(mycopy:))

    {

       return YES;

    }

   else

    {

       return [supercanPerformAction:action withSender:sender];

        

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值