欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中管理剪切板的UIPasteboard粘貼板類用法詳解

 更新時間:2016年06月18日 09:41:08   作者:琿少  
在iOS中,通過UITextField、UITextView和UIWebView剪切或復制的內(nèi)容都可以通過UIPasteboard類來管理粘貼操作,下面就為大家?guī)韎OS中管理剪切板的UIPasteboard粘貼板類用法詳解:

一、自帶剪切板操作的原生UI控件
在iOS的UI系統(tǒng)中,有3個控件自帶剪切板操作,分別是UITextField、UITextView與UIWebView。在這些控件的文字交互處進行長按手勢可以在屏幕視圖上喚出系統(tǒng)的剪切板控件,用戶可以進行復制、粘貼,剪切等操作,其效果分別如下圖所示。

201661893404792.png (300×533)

UITextField的文字操作

201661893437177.png (300×533)

UITextView的文字操作

201661893503118.png (300×533)

二、系統(tǒng)的剪切板管理類UIPasteboard

實際上,當用戶通過上面的空間進行復制、剪切等操作時,被選中的內(nèi)容會被存放到系統(tǒng)的剪切板中,并且這個剪切板并不只能存放字符串數(shù)據(jù),其還可以進行圖片數(shù)據(jù)與網(wǎng)址URL數(shù)據(jù)的存放。這個剪切板就是UIPasteboard類,開發(fā)者也可以直接通過它來操作數(shù)據(jù)進行應用內(nèi)或應用間傳值。

UIPasteboard類有3個初始化方法,如下:

//獲取系統(tǒng)級別的剪切板
+ (UIPasteboard *)generalPasteboard;
//獲取一個自定義的剪切板 name參數(shù)為此剪切板的名稱 create參數(shù)用于設置當這個剪切板不存在時 是否進行創(chuàng)建
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
//獲取一個應用內(nèi)可用的剪切板
+ (UIPasteboard *)pasteboardWithUniqueName;

上面3個初始化方法,分別獲取或創(chuàng)建3個級別不同的剪切板,系統(tǒng)級別的剪切板在整個設備中共享,即是應用程序被刪掉,其向系統(tǒng)級的剪切板中寫入的數(shù)據(jù)依然在。自定義的剪切板通過一個特定的名稱字符串進行創(chuàng)建,它在應用程序內(nèi)或者同一開發(fā)者開發(fā)的其他應用程序中可以進行數(shù)據(jù)共享。第3個方法創(chuàng)建的剪切板等價為使用第2個方法創(chuàng)建的剪切板,只是其名稱字符串為nil,它通常用于當前應用內(nèi)部。

注意:使用第3個方法創(chuàng)建的剪切板默認是不進行數(shù)據(jù)持久化的,及當應用程序退出后,剪切板中內(nèi)容將別抹去。若要實現(xiàn)持久化,需要設置persistent屬性為YES。

UIPasteboard中常用方法及屬性如下:

//剪切板的名稱
@property(readonly,nonatomic) NSString *name;
//根據(jù)名稱刪除一個剪切板
+ (void)removePasteboardWithName:(NSString *)pasteboardName;
//是否進行持久化
@property(getter=isPersistent,nonatomic) BOOL persistent;
//此剪切板的改變次數(shù) 系統(tǒng)級別的剪切板只有當設備重新啟動時 這個值才會清零
@property(readonly,nonatomic) NSInteger changeCount;

下面這些方法用于設置與獲取剪切板中的數(shù)據(jù):

最新一組數(shù)據(jù)對象的存取:

//獲取剪切板中最新數(shù)據(jù)的類型
- (NSArray<NSString *> *)pasteboardTypes;
//獲取剪切板中最新數(shù)據(jù)對象是否包含某一類型的數(shù)據(jù)
- (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes;
//將剪切板中最新數(shù)據(jù)對象某一類型的數(shù)據(jù)取出
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
//將剪切板中最新數(shù)據(jù)對象某一類型的值取出
- (nullable id)valueForPasteboardType:(NSString *)pasteboardType;
//為剪切板中最新數(shù)據(jù)對應的某一數(shù)據(jù)類型設置值
- (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType;
//為剪切板中最新數(shù)據(jù)對應的某一數(shù)據(jù)類型設置數(shù)據(jù)
- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;
多組數(shù)據(jù)對象的存?。?

//數(shù)據(jù)組數(shù)
@property(readonly,nonatomic) NSInteger numberOfItems;
//獲取一組數(shù)據(jù)對象包含的數(shù)據(jù)類型
- (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet;
//獲取一組數(shù)據(jù)對象中是否包含某些數(shù)據(jù)類型
- (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes inItemSet:(nullable NSIndexSet *)itemSet;
//根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)對象
- (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes;
//根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)的值
- (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
//根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)的NSData數(shù)據(jù)
- (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
//所有數(shù)據(jù)對象
@property(nonatomic,copy) NSArray *items;
//添加一組數(shù)據(jù)對象
- (void)addItems:(NSArray<NSDictionary<NSString *, id> *> *)items;

上面方法中很多需要傳入數(shù)據(jù)類型參數(shù),這些參數(shù)是系統(tǒng)定義好的一些字符竄,如下:

//所有字符串類型數(shù)據(jù)的類型定義字符串數(shù)組
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListString;
//所有URL類型數(shù)據(jù)的類型定義字符串數(shù)組
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListURL;
//所有圖片數(shù)據(jù)的類型定義字符串數(shù)據(jù)
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListImage;
//所有顏色數(shù)據(jù)的類型定義字符串數(shù)組
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListColor;


相比于上面兩組方法,下面這些方法更加面向?qū)ο?,在開發(fā)中使用更加方便與快捷:

//獲取或設置剪切板中的字符串數(shù)據(jù)
@property(nullable,nonatomic,copy) NSString *string;
//獲取或設置剪切板中的字符串數(shù)組
@property(nullable,nonatomic,copy) NSArray<NSString *> *strings;
//獲取或設置剪切板中的URL數(shù)據(jù)
@property(nullable,nonatomic,copy) NSURL *URL;
//獲取或設置剪切板中的URL數(shù)組
@property(nullable,nonatomic,copy) NSArray<NSURL *> *URLs;
//獲取或s何止剪切板中的圖片數(shù)據(jù)
@property(nullable,nonatomic,copy) UIImage *image;
//獲取或設置剪切板中的圖片數(shù)組
@property(nullable,nonatomic,copy) NSArray<UIImage *> *images;
//獲取或設置剪切板中的顏色數(shù)據(jù)
@property(nullable,nonatomic,copy) UIColor *color;
//獲取或設置剪切板中的顏色數(shù)組
@property(nullable,nonatomic,copy) NSArray<UIColor *> *colors;
對剪切板的某些操作會觸發(fā)如下通知:

//剪切板內(nèi)容發(fā)生變化時發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedNotification;
//剪切板數(shù)據(jù)類型鍵值增加時發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey;
//剪切板數(shù)據(jù)類型鍵值移除時發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey;
//剪切板被刪除時發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;

三、復制圖片的簡單例子
創(chuàng)建一個CopyView

#import "CopyView.h"
@interface CopyView ()
@property (strong, nonatomic) UIImageView* img1;
@property (strong, nonatomic) UIImageView* img2;
@end

@implementation CopyView
-(UIImageView *)img1{
  if (_img1 == nil) {
    _img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"NetworldImage" ofType:@"jpg"];
    _img1.image = [UIImage imageWithContentsOfFile:path];
  }
  return _img1;
}

-(UIImageView *)img2{
  if (_img2 == nil) {
     _img2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.img1.frame)+50.0f, 20.0f, 100.0f, 100.0f)];
    _img2.backgroundColor = [UIColor lightGrayColor];
  }
  return _img2;
}

- (instancetype)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.img1];
    [self addSubview:self.img2];
  }
  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 setImage:self.img1.image];
}

-(void)paste:(id)sender{
  UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  self.img2.image = [pasteboard image];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  [self becomeFirstResponder];
  UIMenuController* menuController = [UIMenuController sharedMenuController];
  [menuController setTargetRect:self.img1.frame inView:self];
  [menuController setMenuVisible:YES animated:YES];
}

@end
在controller中

#import "ViewController.h"
#import "CopyView.h"

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  CopyView* cv = [[CopyView alloc] initWithFrame:self.view.bounds];
  self.view = cv;
}

@end

效果展示

201661893535077.png (592×986)

201661893556615.png (592×986)

相關(guān)文章

  • iOS中l(wèi)ebel特殊字符的自動換行問題解決

    iOS中l(wèi)ebel特殊字符的自動換行問題解決

    這篇文章主要給大家介紹了關(guān)于iOS中l(wèi)ebel特殊字符的實現(xiàn)不自動換行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習iOS具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-10-10
  • 使用objc runtime實現(xiàn)iOS閉環(huán)的懶加載功能

    使用objc runtime實現(xiàn)iOS閉環(huán)的懶加載功能

    利用objc runtime的動態(tài)性實現(xiàn)懶加載可以實現(xiàn)即可增加又可刪除功能,也可以避免污染類型。這篇文章主要介紹了使用objc runtime實現(xiàn)iOS閉環(huán)的懶加載功能,需要的朋友可以參考下
    2019-06-06
  • 深入講解iOS開發(fā)中應用數(shù)據(jù)的存儲方式

    深入講解iOS開發(fā)中應用數(shù)據(jù)的存儲方式

    這篇文章主要介紹了iOS開發(fā)中應用數(shù)據(jù)的存儲方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個部分,需要的朋友可以參考下
    2015-12-12
  • iOS 獲取當前的ViewController的方法

    iOS 獲取當前的ViewController的方法

    本篇文章主要介紹了iOS 獲取當前的ViewController的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • iOS按比例實現(xiàn)方塊圖

    iOS按比例實現(xiàn)方塊圖

    這篇文章主要為大家詳細介紹了iOS按比例實現(xiàn)方塊圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法

    iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法

    這篇文章主要介紹了iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • IOS中UIWebView、WKWebView之JS交互

    IOS中UIWebView、WKWebView之JS交互

    本篇文章主要介紹了IOS中UIWebView、WKWebView之JS交互,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • iOS設置圓角陰影 避免離屏渲染

    iOS設置圓角陰影 避免離屏渲染

    這篇文章主要為大家詳細介紹了iOS設置圓角陰影,避免離屏渲染,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Objective-C Json 實例詳解

    Objective-C Json 實例詳解

    這篇文章主要介紹了 Objective-C Json 實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握Object-C Json的使用,需要的朋友可以參考下
    2017-10-10
  • ios swift3.0實現(xiàn)二維碼掃描、生成、識別示例代碼

    ios swift3.0實現(xiàn)二維碼掃描、生成、識別示例代碼

    本篇文章主要介紹了ios swift3.0實現(xiàn)二維碼掃描、生成、識別示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-02-02

最新評論