zoukankan      html  css  js  c++  java
  • 文本段的总体复制

    在做项目的时候常常遇到一些地方须要复制文本框的内容,尽管简单但还是稍做总结。

    对复制的操作通常是在标签栏上(Label)。响应通常是长按手势(LongPressGesture),其它情况不做赘述。

    首先声明一个方法:

    - (void)copyActionWithView:(UIView *)view WithString:(NSString *)text WithSuperView:(UIView *)superView;
    在标签栏上加入手势,设置可触摸属性:

    _contentLabel.userInteractionEnabled = YES;
    UILongPressGestureRecognizer * longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [_contentLabel addGestureRecognizer:longPressGR];

    - (void)longPress:(UILongPressGestureRecognizer *)sender{
        if (sender.state == UIGestureRecognizerStateBegan) {
            [self copyActionWithView:sender.view WithString:self.contentLabel.text WithSuperView:nil];
        }
    }

    实现:(一般在控制器上)

    - (void)copyActionWithView:(UIView *)view WithString:(NSString *)text WithSuperView:(UIView *)superView{
        [self becomeFirstResponder];
        self.ownCopyString = text;
        UIMenuController *menu = [UIMenuController sharedMenuController];
        UIMenuItem * copy = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyAction:)];
        menu.menuItems = [NSArray arrayWithObjects: copy, nil];
        CGRect targetRect;
        if (superView) {
            CGRect beginRcet = [view convertRect:view.frame toView:superView];
            CGRect minRect = [superView convertRect:superView.frame toView:superView.superview];
            CGRect endRect = [superView.superview convertRect:superView.superview.frame toView:self.view];
            targetRect.origin.y = endRect.origin.y + minRect.origin.y / 2.0 + beginRcet.origin.y / 2.0 + 8;
        }else{
            targetRect = [view convertRect:view.frame toView:self.view];
            targetRect.origin.y -= 20;
        }
        targetRect.origin.x = 110;
        targetRect.size.width = 100;
        targetRect.size.height = 17.1;
        [menu setTargetRect:targetRect inView:self.view];
        [menu setMenuVisible:YES animated:YES];
    }
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        if (action == @selector(copyAction:)) {
            return YES;//显示
        }
        return NO;//不显示
    }
    
    - (BOOL)canBecomeFirstResponder{
        return YES;
    }
    
    - (void)copyAction:(id)sender{
        UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:self.ownCopyString];
    }

    在通常情况下,须要复制的文本都是在较深层次的视图,一般要通过协议实现详细的方法。这里仅仅是简要说明。

    另外还能够自己定义弹出点(复制小窗体)的位置。

    上传一个演示样例。展示一下效果:







  • 相关阅读:
    linux嵌入式终端ssh无法输入中文以及删除中文异常
    DNS解析错误导致无法ping通网络
    嵌入式linux 打开ping端口
    嵌入式Linux vim编辑器支持中文
    常见BUG-Web
    考勤项目
    功能测试报告的编写
    04_postman环境变量和身份验证
    03_postman的collection管理
    02_postman断言
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7261292.html
Copyright © 2011-2022 走看看