UITextView定制弹出菜单

    需求: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 {
    //对应的功能实现体
}

效果图如下:

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

相关阅读更多精彩内容

友情链接更多精彩内容