iOS开发之复制粘贴 剪切

来源:互联网 发布:unity3d 节点 编辑:程序博客网 时间:2024/06/05 10:55
  1. UIPasteboard:剪切板                 用来向其中写入数据以及从中读取数据,从来实现数据的”搬迁”。
  2. UIMenuController:编辑菜单     用来显示拷贝、黏贴等命令
  3. canPerformAction:withSender::用于控制编辑菜单显示的命令按钮。事实上,编辑菜单的命令按钮完全可以自定义,而无需通过实现canPerformAction:方法来控制命令按钮。
  4. UIResponderStandardEditActions:一个非正式协议(UIResponder的类别),申明了几个编辑动作,例如copy:等等。当编辑菜单上的命令被点击时,相关方法会被调用。系统未实现这个方法,必须override!
1。UITableView提供了相应的代理方法用来方便实现cell内容的拷贝粘贴等。必须实现下面3个方法

//长按cell时是否显示编辑菜单

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    if (indexPath.section ==0 && indexPath.row !=0) {

        

        return YES;

        

    }

    return NO;

    

}

//控制编辑菜单中的按钮

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

    if (action == @selector(copy:)) {


        return YES;


    }

    return NO;


}


//执行编辑菜单中点击的按钮动作

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

    if (action == @selector(copy:)) {


        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


        [UIPasteboardgeneralPasteboard].string = cell.textLabel.text;


    }

}


2.UILabel的文本copy

- (instancetype)initWithFrame:(CGRect)frame {

    self = [superinitWithFrame:frame];

    if (self) {

        self.userInteractionEnabled =YES;

        UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(actionShowMenu:)];

        [self addGestureRecognizer:longPressGestureRecognizer];

    }

    return self;

}

- (void)actionShowMenu:(UILongPressGestureRecognizer *)recognizer {

    if (recognizer.state ==UIGestureRecognizerStateBegan) {

        [selfbecomeFirstResponder];

        UIMenuController *menu = [UIMenuControllersharedMenuController];


        //这里可以自定义

//        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(menuCopyBtnPressed:)];

//

//        menuController.menuItems = @[copyItem];



        [menu setTargetRect:CGRectMake([recognizerlocationInView:self].x, [recognizerlocationInView:self].y,0,0)inView:self];

        [menu setMenuVisible:YESanimated:YES];

    }

}


//允许成为第一响应

- (BOOL)canBecomeFirstResponder{

    return YES;

}


- (void)copy:(id)sender{

    [UIPasteboard generalPasteboard].string =self.text;

}


- (void)cut:(id)sender{

    [UIPasteboard generalPasteboard].string =self.text;

    self.text =nil;

}


- (void)paste:(id)sender{

    self.text = [UIPasteboardgeneralPasteboard].string;

}



//  用于控制哪些命令显示在编辑菜单中

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{

    if (action == @selector(copy:)) {

        return YES;

    }else if (action ==@selector(cut:)){

        return YES;

    }else if (action ==@selector(paste:)){

        return YES;

    }

    return [supercanPerformAction:actionwithSender:sender];

}





原创粉丝点击