美文网首页
iOS UIPasteboard

iOS UIPasteboard

作者: 搬砖的crystal | 来源:发表于2023-02-20 15:52 被阅读0次

    一、简介

    1.创建

    UIPasteboard 是 iOS 中访问粘贴板的原生控件,可分为系统等级的和 APP 等级的,系统等级的独立于 APP,可以复制一个 APP 的内容到另一个 APP ;APP 等级的只能在 APP 内进行复制和粘贴。

        //系统级
        UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:@"系统"];
        NSLog(@"%@",pasteboard.string);
        
        //APP级
        UIPasteboard * pasteboard1 = [UIPasteboard pasteboardWithName:@"test" create:YES];
        [pasteboard1 setString:@"app"];
        NSLog(@"%@",pasteboard1.string);
    }
    
    2.数据类型

    可以复制在粘贴板的数据类型有 NSStringUIImageNSURLUIColorNSData 以及由这些类型元素组成的数组。可分别由它们的 set 方法将数据放在粘贴板中。

    3.常用的方法
    /*通过名称获取粘贴板并且移除*/
    + (void)removePasteboardWithName:(NSString *)pasteboardName; 
    /*从粘贴板中获取数据,pasteboardType是自定义的,说明app可以处理哪种类型的数据*/
    - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType; 
    /*data类型的数据放在粘贴板中,pasteboardType同上*/
    - (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;
    /*从粘贴板中取出data*/
    - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
    
    4.使用方法

    在 iOS 中,支持 UIPasteboard 原生控件只有 UITextFieldUITextViewUIWebView 这三个,如果想自定义一个控件能够使用 UIPasteboard,需要在定义的时候重载 -(BOOL)canBecomeFirstResponder-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 这两个方法,前者设置控件可称为第一响应器,后者决定这个控件能够使用复制、剪切、选中、全选、粘贴等的哪一种或几种功能,并重载对应的 -(void)copy:(id)sender-(void)cut:(id)sender-(void)select:(id)sender-(void)selectAll:(id)sender-(void)paste:(id)sender 方法,在这几个方法中处理事件,UIMenuController 负责显示 UI

    二、示例

    DJCopyView

    #import <UIKit/UIKit.h>
    
    @interface DJCopyView : UIView
    
    @end
    
    #import "DJCopyView.h"
    @interface DJCopyView()
    
    @property ( nonatomic,strong) UILabel *label1;
    @property ( nonatomic,strong) UILabel *label2;
    
    @end
    
    @implementation DJCopyView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor orangeColor];
            self.label1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, frame.size.width - 30, 30)];
            self.label1.text = @"texttext";
            self.label1.backgroundColor = [UIColor blueColor];
            [self addSubview:self.label1];
            
            self.label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, 50, frame.size.width - 30, 30)];
            [self addSubview:self.label2];
            self.label2.backgroundColor = [UIColor blueColor];
        }
        return self;
    }
    
    -(BOOL)canBecomeFirstResponder{
        return YES;
    }
    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
        NSArray* methodNameArr = @[@"copy:",@"cut:",@"select:",@"selectAll:",@"paste:"];
        if ([methodNameArr containsObject:NSStringFromSelector(action)]) {
            return YES;
        }
        return [super canPerformAction:action withSender:sender];
    }
    
    -(void)copy:(id)sender{
        UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
        [pasteboard setString:self.label1.text];
    }
    
    -(void)paste:(id)sender{
        UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
        self.label2.text = [pasteboard string];
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self becomeFirstResponder];
        UIMenuController* menuController = [UIMenuController sharedMenuController];
        [menuController showMenuFromView:self rect:self.label1.frame];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS UIPasteboard

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