注:仅仅是提供思路 有些问题如隐藏系统自带的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]];
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];
}
}