UITextView定制弹出菜单

作者: 跑调的安眠曲 | 来源:发表于2016-03-04 16:00 被阅读315次

        需求:UITextView选中文字之后,需要定制一些功能,屏蔽系统的某些功能。

    自定义一个UITextView的子类YQTextView

    屏蔽系统菜单栏#####
    #import "YQTextView.h"
    
    @implementation YQTextView
    
    #pragma mark - 选中文字后是否能够弹出菜单
    - (BOOL)canBecameFirstResponder {
        return YES;
    }
    
    #pragma mark - 选中文字后的系统菜单响应的选项
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if (action == @selector(copy:)) {
            return YES;
        } else if (action == @selector(selectAll:)) {
            return YES;
        }
        return NO;
    }
    
    @end
    
    定制弹出菜单栏#####

    在有需求的ViewController的viewDidLoad方法中添加如下方法,并增加实现方法(若没有定义实现方法,那么这功能菜单是不会显示的)

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIMenuItem *menuOneItem = [[UIMenuItem alloc] initWithTitle:@"菜单一" action:@selector(oneAction:)];
        UIMenuItem *menuTwoItem = [[UIMenuItem alloc] initWithTitle:@"菜单二" action:@selector(twoAction:)];
        [UIMenuController sharedMenuController].menuItems = @[menuOneItem, menuTwoItem];
    }
    #pragma mark - 菜单按钮的实现方法
    - (void)oneAction:(id)sender {
        //对应的功能实现体
    }
    
    - (void)twoAction:(id)sender {
        //对应的功能实现体
    }
    
    

    效果图如下:

    效果图

    相关文章

      网友评论

        本文标题:UITextView定制弹出菜单

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