iOS - UIMenuController:UITextField去掉拷贝、粘贴,UILabel自定义长按拷贝

时间:2022-11-06 23:46:59

在使用微信过程中 你会发现长按消息会弹出复制、粘贴、撤回这样的选项并且带有箭头,在iOS开发中,实现这样的功能我们要用到的是UIMenuController,在iOS中,UITextField、UITextView都会有默认的UIMenuController,但是有时候我们并不希望使用这样的功能,输入框的粘贴复制往往能越过代理,导致我们不能有效的监控输入的内容,所以我们要去掉复制粘贴选项。我们需要自定义输入框。

1. 自定义一个没有复制剪贴的UITextField

1.1 创建一个类,继承自UITextField

//类名随意
@interface MJWNoCopyTextField : UITextField

@end

1.2 UIMenuController设置为不可用

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //Initialzation code
    }
    return self;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
    //设置为不可用
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

1.3 使用自定义的TextField

#pragma mark - 
#pragma mark 自定义去掉拷贝TextField
    MJWNoCopyTextField *noCopyTF = [[MJWNoCopyTextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 250)/2, 300, 250, 35)];
    noCopyTF.backgroundColor = [UIColor whiteColor];
    noCopyTF.layer.cornerRadius = 4;
    noCopyTF.layer.masksToBounds = YES;
    noCopyTF.font = [UIFont systemFontOfSize:14];
    noCopyTF.layer.borderWidth = 1;
    noCopyTF.layer.borderColor = [[[UIColor lightGrayColor] colorWithAlphaComponent:0.4] CGColor];
    noCopyTF.placeholder = @"不能粘贴或者拷贝";
    [self.view addSubview:noCopyTF];

1.4 效果图

我们单机、双击、右击、长按,输入框都不会再弹出UIMenuController。
iOS - UIMenuController:UITextField去掉拷贝、粘贴,UILabel自定义长按拷贝

2. 自定义一个长按复制UILabel

在iOS中,UILabel不具备复制粘贴功能,需要我们自定义。

2.1 创建一个继承自UILabel的类

//类名请随意
@interface MJWCustomerMeauControllerLabel : UILabel

@end

2.2 其中的几个方法

//第一响应(否则不会生效)

-(BOOL)canBecomeFirstResponder {
    return YES;
}

//可响应的方法

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender { return (action == @selector(copy:)); }

//响应方法

-(void)copy:(id)sender {
    /* 拷贝结束,颜色恢复 */
    self.backgroundColor = [UIColor whiteColor];
    /* 剪切板 */
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.text;

}

//添加长按手势

-(void)attachTapHandler {
    /* 长按手势 */
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *touch = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self addGestureRecognizer:touch];
}

//绑定事件

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self attachTapHandler];
    }
    return self;
}
-(void)awakeFromNib {

    [super awakeFromNib];
    [self attachTapHandler];
}

//长按手势响应

-(void)handleTap:(UIGestureRecognizer*) recognizer {

    [self becomeFirstResponder];
    //监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidden) name:UIMenuControllerWillHideMenuNotification object:nil];
    self.backgroundColor = [UIColor colorWithRed:0.231 green:0.843 blue:1.000 alpha:0.500];
    UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"复制"
                                                      action:@selector(copy:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];
}

//消失恢复原样

- (void)hidden
{
    self.backgroundColor = [UIColor whiteColor];
}
- (void)dealloc{
    // 移除监听通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

2.3 效果图

iOS - UIMenuController:UITextField去掉拷贝、粘贴,UILabel自定义长按拷贝

该篇博文简单的说明了UIMenuController的使用方法,如有更加详尽的还望各位补充。