iOS-UIMenuController的使用:用来复制,粘贴,删除等内容的操作

概念:

UIMenuController即菜单控制器,是一个单例对象,用来复制,粘贴,删除等内容的操作。

1、实例:WKWebViewUITextView

调用方法addSelectedContentView:即可

#pragma mark -- UIMenuController,(UIMenuController即菜单控制器,是一个单例对象,用来复制,粘贴,删除等内容的操作)
- (void)addSelectedContentView:(UIview*)view {
    
    // 给WKWebView添加UIMenuItem
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.view.frame inView:self.view];
    
    // 添加两个新的菜单:翻译、7500查询
    UIMenuItem *translateItem = [[UIMenuItem alloc] initWithTitle:@"翻译" action:@selector(translateSelected:)];
    UIMenuItem *shiyiItem = [[UIMenuItem alloc] initWithTitle:@"释义" action:@selector(shiYiSelected:)];
    // 自定义菜单添加到菜单栏中
//    [menu setMenuItems:@[translateItem, shiyiItem]];
    menu.menuItems = @[translateItem, shiyiItem];
    menu.menuVisible = YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    // 该方法与编辑菜单相关,与返回YES的方法关联的菜单将显示出来。
    if (action == @selector(translateSelected:) || action == @selector(shiYiSelected:)) {
        return YES;
    } else {
        return NO;
    }
}

- (void)translateSelected:(id)sender {
    // 翻译取词
    
    // 获取选择的文本
    /// 1、这是在UITextView上,选取文本
    /// NSString *selectString = [self.textView.text substringWithRange:self.textView.selectedRange];
    /// self.textView.selectedRange= NSMakeRange(0, 0); // 清除选中的内容
    /// 2、这是在WKWebView上,选取文本
    NSString *jsCript = @"window.getSelection().toString()";
    [self.webView evaluateJavaScript:jsCript completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        NSLog(@"%@", result); // 选中的文本:result
    }];
    
}

2、实例:UILabel:这里是写在UILabel的分类中(不常用可以随意添加手势)

原理:给UILabel添加长按手势

#import "UILabel+WLCopy.h"

@implementation UILabel (WLCopy)


// 初始化设置长按操作
- (void)pressAction {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    longPress.minimumPressDuration = 1;
    [self addGestureRecognizer:longPress];
}

- (void)longPressAction:(UIGestureRecognizer *)recognizer {
    [self becomeFirstResponder];
    UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"拷贝" action:@selector(customCopy:)];
    UIMenuItem *soundItem = [[UIMenuItem alloc] initWithTitle:@"句段发音" action:@selector(customSound:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyItem,soundItem, nil]];
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}

// 使label能够成为响应事件
- (BOOL)canBecomeFirstResponder {
    return YES;
}
// 控制响应的方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if(action == @selector(customCopy:))
    {
        return YES;
    }else if(action == @selector(customSound:))
    {
        return YES;
    }
    
    return NO;
}

- (void)customCopy:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.text;
}

- (void)customSound:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SentenceSoundNotification" object:[NSString stringWithFormat:@"%@",self.text]];
}




@end

其他人的处理:https://www.jianshu.com/p/3e08d9ce201a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,879评论 28 54
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,391评论 4 8
  • 怎么对待生活,它也会怎么对你 人都是哭着来到这个美丽的人间。每个人从来到尘寰到升入天堂,整个生命的历程都是一本书,...
    静静在等你阅读 5,331评论 1 6

友情链接更多精彩内容