每個(gè)程序猿都是一名強(qiáng)大的戰(zhàn)士,名曰CV戰(zhàn)士.

序言
如上圖,我們?cè)趹?yīng)用程序中時(shí)常需要有黏貼復(fù)制的功能,像UIWebView,UITextField,UITextView這些類都是自帶黏貼復(fù)制功能的,但是我們先讓某個(gè)控件具有黏貼復(fù)制功能,我們?cè)撛趺崔k呢?那就需要用到兩個(gè)類,一個(gè)是UIPasteboard,另外一個(gè)就是UIMenuController,一個(gè)是剪切板,另外一個(gè)是菜單彈窗.
從UIMenuController到菜單彈窗的完美變身
UIMenuController的用法和UIAlertView的使用方法是類似的,如下圖,我在storyboard的ViewController的控制器中,添加兩個(gè)個(gè)UILabel對(duì)象,然后在ViewController其中添加手勢(shì).

把UILabel拖成屬性,然后代碼如下
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *testlLabel;//測(cè)試Label
@property (strong, nonatomic) IBOutlet UILabel *pasteLabel;//黏貼Label
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadingTapGR];
}
//添加點(diǎn)擊手勢(shì)
-(void)loadingTapGR{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(showMenuVC:)];
longPress.minimumPressDuration = 1;
self.testlLabel.userInteractionEnabled = YES;
[self.testlLabel addGestureRecognizer:longPress];
UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(showMenuVC:)];
longPress2.minimumPressDuration = 1;
self.pasteLabel.userInteractionEnabled = YES;
[self.pasteLabel addGestureRecognizer:longPress2];
}
//顯示菜單
-(void)showMenuVC:(UILongPressGestureRecognizer *)longPress{
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem * menuItem = [[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(copyTitle)];
UIMenuItem * pasteItem = [[UIMenuItem alloc] initWithTitle:@"黏貼" action:@selector(paste:)];
NSLog(@"%@",menuController.menuItems);
[menuController setMenuItems:@[menuItem,pasteItem]];
CGPoint location = [longPress locationInView:[longPress view]];
CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
[menuController setTargetRect:menuLocation inView:[longPress view]];
[menuController setMenuVisible:YES animated:YES];
}
//復(fù)制文本
-(void)copyTitle{
}
//黏貼
-(void)paste:(id)sender
{
}
@end
代碼是如上實(shí)現(xiàn)了,但是我們點(diǎn)擊Label仍是沒有菜單出現(xiàn),這是為什么呢?因?yàn)槲覀冞€重新定義下面的這個(gè)方法.
//允許成為第一響應(yīng)者
-(BOOL)canBecomeFirstResponder{
return YES;
}
看一下效果圖,好了菜單完成了,我們接下來要做復(fù)制黏貼的功能了.

從UIPasteboard到復(fù)制黏貼功能的終極進(jìn)化
在使用UIPasteboard之前,我們需要先了解一下剪貼板UIPasteboard的一些基本的知識(shí).
剪貼板類型:
系統(tǒng)級(jí)別:使用UIPasteboardNameGeneral和UIPasteboardNameFind,系統(tǒng)級(jí)應(yīng)用程序關(guān)閉,或者卸載的數(shù)據(jù)不會(huì)丟失。
應(yīng)用程序級(jí):通過設(shè)置,可以讓數(shù)據(jù)在應(yīng)用程序關(guān)閉之后仍然保存在剪貼板中,但是應(yīng)用程序卸載之后數(shù)據(jù)就會(huì)失去。我們可用通過pasteboardWithName:create:來創(chuàng)建。
剪貼板可直接存放的類型
1、UIPasteboardTypeListString — 字符串?dāng)?shù)組 包含kUTTypeUTF8PlainText
2、UIPasteboardTypeListURL — URL數(shù)組,包含kUTTypeURL
3、UIPasteboardTypeListImage — 圖形數(shù)組, 包含kUTTypePNG 和kUTTypeJPEG
4、UIPasteboardTypeListColor — 顏色數(shù)組
下面我們對(duì)上面代碼中的 -(void)copyTitle 方法進(jìn)行進(jìn)一步的完善.使其能有簡單的復(fù)制功能.
//復(fù)制文本
-(void)copyTitle{
NSLog(@"復(fù)制功能的實(shí)現(xiàn)~");
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];//普通的黏貼板
pasteboard.string = self.testlLabel.text;
}
復(fù)制功能實(shí)現(xiàn)之后,我們需要實(shí)現(xiàn)黏貼功能.
-(void)paste:(id)sender
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
//判斷是否數(shù)據(jù)
if (nil != pboard.string) {
self.pasteLabel.text = pboard.string;
}
}
到此,復(fù)制黏貼的功能就實(shí)現(xiàn)了,當(dāng)然了,這是對(duì)一些簡單對(duì)象(比如:string,URL,image,color)進(jìn)行的復(fù)制黏貼,如果是復(fù)雜對(duì)象,改如何進(jìn)行黏貼復(fù)制呢?這時(shí)候就需要使用到歸檔和反歸檔把數(shù)據(jù)變成NSData類型的對(duì)象,然后使用 setData:forPasteboardType: 和 dataForPasteboardType: 兩個(gè)方法進(jìn)行數(shù)據(jù)的存儲(chǔ)和獲取,代碼如下
//存儲(chǔ)數(shù)據(jù)
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
NSDictionary *dic = [NSDictionary dictionaryWithObject:self.testlLabel.text forKey:@"saoDong"];
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dic];
[pb setData:dictData forPasteboardType:@"myType"];
//獲取就類似于這樣:
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForPasteboardType:@"myType"]];
self.pasteLabel.text = [dict objectForKey:@"saoDong"];
總結(jié): UIPasteboard和UIMenuController兩個(gè)類整體使用起來比較簡單,難度一般,如果有什么疑問,請(qǐng)?jiān)谙旅嬖u(píng)論,我會(huì)及時(shí)回復(fù).謝謝您的查看,最后附上測(cè)試的Demo.