美文网首页iOS
ios UILabel长按复制

ios UILabel长按复制

作者: SunnyLeong | 来源:发表于2017-11-13 11:03 被阅读322次

    在iOS中下面三个控件,自身就有复制-粘贴的功能:

    • 1、UITextView
    • 2、UITextField
    • 3、UIWebView

    UIKit framework提供了几个类和协议方便我们在自己的应用程序中实现剪贴板的功能。

    • 1、UIPasteboard:我们可以向其中写入数据,也可以读取数据
    • 2、UIMenuController:显示一个快捷菜单,用来展示复制、剪贴、粘贴等选择的项。
    • 3、UIResponder中的canPerformAction:withSender:用于控制哪些命令显示在快捷菜单中。
    • 4、当快捷菜单上的命令点击的时候,UIResponderStandardEditActions将会被调用。

    下面这些项能被放置到剪贴板中

    • 1、UIPasteboardTypeListString — 字符串数组, 包含kUTTypeUTF8PlainText
    • 2、UIPasteboardTypeListURL — URL数组,包含kUTTypeURL
    • 3、UIPasteboardTypeListImage — 图形数组, 包含kUTTypePNG 和kUTTypeJPEG
    • 4、UIPasteboardTypeListColor — 颜色数组

    剪贴板的类型分为两种:

    • 系统级:使用UIPasteboardNameGeneral和UIPasteboardNameFind,系统级应用程序关闭,或者卸载的数据不会丢失。
    • 应用程序级:通过设置,可以让数据在应用程序关闭之后仍然保存在剪贴板中,但是应用程序卸载之后数据就会失去。我们可用通过pasteboardWithName:create:来创建。

    系统默认支持提供的按钮触发方法(UIResponderStandardEditActions)列举如下:

    (方法声明在UIResponder.h头文件中)

    //剪切按钮的方法
    - (void)cut:(nullable id)sender NS_AVAILABLE_IOS(3_0);
    //复制按钮的方法
    - (void)copy:(nullable id)sender NS_AVAILABLE_IOS(3_0);
    //粘贴按钮的方法
    - (void)paste:(nullable id)sender NS_AVAILABLE_IOS(3_0);
    //选择按钮的方法
    - (void)select:(nullable id)sender NS_AVAILABLE_IOS(3_0);
    //全选按钮的方法
    - (void)selectAll:(nullable id)sender NS_AVAILABLE_IOS(3_0);
    //删除按钮的方法
    - (void)delete:(nullable id)sender NS_AVAILABLE_IOS(3_2);
    //改变书写模式为从左向右按钮触发的方法
    - (void)makeTextWritingDirectionLeftToRight:(nullable id)sender NS_AVAILABLE_IOS(5_0);
    //改变书写模式为从右向左按钮触发的方法
    - (void)makeTextWritingDirectionRightToLeft:(nullable id)sender NS_AVAILABLE_IOS(5_0);
    // 切换字体为黑体(粗体)
    - (void)toggleBoldface:(nullable id)sender NS_AVAILABLE_IOS(6_0);
    // 切换字体为斜体
    - (void)toggleItalics:(nullable id)sender NS_AVAILABLE_IOS(6_0);
    // 给文字添加下划线
    - (void)toggleUnderline:(nullable id)sender NS_AVAILABLE_IOS(6_0);
    // 增加字体大小
    - (void)increaseSize:(nullable id)sender NS_AVAILABLE_IOS(7_0);
    // 减小字体大小
    - (void)decreaseSize:(nullable id)sender NS_AVAILABLE_IOS(7_0);
    

    关于UIMenuController上面的按钮,系统中还有许多私有方法,列举如下:

    //替换按钮
    - (void)_promptForReplace:(id)arg1{
      NSLog(@"promptForReplace");
    }
    //简体繁体转换按钮
    -(void)_transliterateChinese:(id)sender{
      NSLog(@"transliterateChinese");
    }
    //文字风格按钮
    -(void)_showTextStyleOptions:(id)sender{
      NSLog(@"showTextStyleOptions");
    }
    //定义按钮
    -(void)_define:(id)sender{
      NSLog(@"define");
    }
    -(void)_addShortcut:(id)sender{
      NSLog(@"addShortcut");
    }
    -(void)_accessibilitySpeak:(id)sender{
      NSLog(@"accessibilitySpeak");
    }
    //语言选择按钮
    -(void)_accessibilitySpeakLanguageSelection:(id)sender{
      NSLog(@"accessibilitySpeakLanguageSelection");
    }
    //暂停发音按钮
    -(void)_accessibilityPauseSpeaking:(id)sender{
      NSLog(@"accessibilityPauseSpeaking");
    }
    //分享按钮
    -(void)_share:(id)sender{
      NSLog(@"share");
    }
    

    UIMenuController还有如下的属性用来设置其显示的位置:

    //显示的位置
    @property(nonatomic) UIMenuControllerArrowDirection arrowDirection;
    //枚举如下:
    typedef NS_ENUM(NSInteger, UIMenuControllerArrowDirection) {
      //默认 基于当前屏幕状态
      UIMenuControllerArrowDefault, // up or down based on screen location
      //箭头在上的显示模式
      UIMenuControllerArrowUp NS_ENUM_AVAILABLE_IOS(3_2),
      //箭头在下的显示模式
      UIMenuControllerArrowDown NS_ENUM_AVAILABLE_IOS(3_2),
      //箭头在左的显示模式
      UIMenuControllerArrowLeft NS_ENUM_AVAILABLE_IOS(3_2),
      //箭头在右的显示模式
      UIMenuControllerArrowRight NS_ENUM_AVAILABLE_IOS(3_2),
    };
    

    自定义文件如下:

    Object-C :

    CopyLabel.h文件

    #import <UIKit/UIKit.h>
    @interface CopyLabel : UILabel
    - (instancetype)init;
    @end
    

    CopyLabel.m文件

    #import "CopyLabel.h"
    
    @implementation CopyLabel
    
    #pragma mark - ---------- 两种常用初始化方法 ----------
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            [self setUp];
        }
        return self;
    }
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            [self setUp];
        }
        return self;
    }
    
    // 设置label可以成为第一响应者
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    // 设置长按事件
    - (void)setUp {
        /* 你可以在这里添加一些代码,比如字体、居中、夜间模式等 */
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)]];
    }
    
    // 长按事件
    - (void)longPress {
        // 设置label为第一响应者
        [self becomeFirstResponder];
        // 自定义 UIMenuController
        UIMenuController * menu = [UIMenuController sharedMenuController];
        UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyText:)];
        menu.menuItems = @[item1];
        [menu setTargetRect:self.bounds inView:self];
        [menu setMenuVisible:YES animated:YES];
    }
    
    // 设置label能够执行那些具体操作
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if(action == @selector(copyText:)) return YES;
        return NO;
    }
    
    // 复制方法
    - (void)copyText:(UIMenuController *)menu {
        // 没有文字时结束方法
        if (!self.text) return;
        // 复制文字到剪切板
        UIPasteboard * paste = [UIPasteboard generalPasteboard];
        paste.string = self.text;
    }
    
    @end
    

    Swift :

    CopyLabel.swift文件

    import Foundation
    import UIKit
    import ObjectiveC
    
    private var is_copyEnabled = false
    
    extension UILabel {
        
        var isCopyEnabled: Bool {
            get{
                return objc_getAssociatedObject(self, &is_copyEnabled) as! Bool
            }
            set{
                objc_setAssociatedObject(self, &is_copyEnabled, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
                attachTapHandler()
            }
        }
        
        open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            return action == #selector(UILabel.copyText(sender:))
        }
        
        func attachTapHandler() {
            self.isUserInteractionEnabled = true
            let longPress = UILongPressGestureRecognizer(target: self, action: #selector(UILabel.handleTap(ges:)))
            self.addGestureRecognizer(longPress)
        }
        
        @objc fileprivate func handleTap(ges: UIGestureRecognizer) {
            if ges.state == .began {
                becomeFirstResponder()
                let item = UIMenuItem(title: "复制", action: #selector(UILabel.copyText(sender:)))
                UIMenuController.shared.menuItems = [item]
                //计算label真实frame,让复制显示在中间
                let rect = (text! as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height:self.bounds.size.height), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: self.font], context: nil)
                let width = rect.size.width > self.bounds.size.width ? self.bounds.size.width : rect.size.width
                let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: width, height: self.frame.size.height)
                UIMenuController.shared.setTargetRect(frame, in: self.superview!)
                UIMenuController.shared.setMenuVisible(true, animated: true)
            }
            
        }
        
        @objc fileprivate func copyText(sender: Any) {
            //通用粘贴板
            let pBoard = UIPasteboard.general
            
            //有时候只想取UILabel得text中一部分
            if objc_getAssociatedObject(self, "expectedText") != nil {
                pBoard.string = objc_getAssociatedObject(self, "expectedText") as! String?
            } else {
                if self.text != nil {
                    pBoard.string = self.text
                } else {
                    pBoard.string = self.attributedText?.string
                }
            }
        }
        
        open override var canBecomeFirstResponder: Bool{
            return isCopyEnabled
        }
        
    }
    

    如何调用:

    Object-C :

        CGRect rect = CGRectMake(self.view.frame.size.width/2 - 80, 200, 160, 25);
        CopyLabel *label = [[CopyLabel alloc] initWithFrame:rect];
        label.text = @"长按我弹出复制按钮";
        label.backgroundColor = [UIColor cyanColor];
        [self.view addSubview:label];
    

    Swift :

        @IBOutlet weak var demoLabel: UILabel!{
            didSet{
                demoLabel.isCopyEnabled = true
            }
        }
    

    至此结束!!!谢谢---

    相关文章

      网友评论

        本文标题:ios UILabel长按复制

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