每個程序猿都是一名強大的戰士,名曰CV戰士.
序言
如上圖,我們在應用程序中時常需要有黏貼復制的功能,像UIWebView,UITextField,UITextView這些類都是自帶黏貼復制功能的,但是我們先讓某個控件具有黏貼復制功能,我們該怎么辦呢?那就需要用到兩個類,一個是UIPasteboard,另外一個就是UIMenuController,一個是剪切板,另外一個是菜單彈窗.
從UIMenuController到菜單彈窗的完美變身
UIMenuController的用法和UIAlertView的使用方法是類似的,如下圖,我在storyboard的ViewController的控制器中,添加兩個個UILabel對象,然后在ViewController其中添加手勢.
把UILabel拖成屬性,然后代碼如下
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *testlLabel;//測試Label
@property (strong, nonatomic) IBOutlet UILabel *pasteLabel;//黏貼Label
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadingTapGR];
}
//添加點擊手勢
-(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:@"復制" 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];
}
//復制文本
-(void)copyTitle{
}
//黏貼
-(void)paste:(id)sender
{
}
@end
代碼是如上實現了,但是我們點擊Label仍是沒有菜單出現,這是為什么呢?因為我們還重新定義下面的這個方法.
//允許成為第一響應者
-(BOOL)canBecomeFirstResponder{
return YES;
}
看一下效果圖,好了菜單完成了,我們接下來要做復制黏貼的功能了.
從UIPasteboard到復制黏貼功能的終極進化
在使用UIPasteboard之前,我們需要先了解一下剪貼板UIPasteboard的一些基本的知識.
剪貼板類型:
系統級別:使用UIPasteboardNameGeneral和UIPasteboardNameFind,系統級應用程序關閉,或者卸載的數據不會丟失。
應用程序級:通過設置,可以讓數據在應用程序關閉之后仍然保存在剪貼板中,但是應用程序卸載之后數據就會失去。我們可用通過pasteboardWithName:create:來創建。
剪貼板可直接存放的類型
1、UIPasteboardTypeListString — 字符串數組 包含kUTTypeUTF8PlainText
2、UIPasteboardTypeListURL — URL數組,包含kUTTypeURL
3、UIPasteboardTypeListImage — 圖形數組, 包含kUTTypePNG 和kUTTypeJPEG
4、UIPasteboardTypeListColor — 顏色數組
下面我們對上面代碼中的 -(void)copyTitle 方法進行進一步的完善.使其能有簡單的復制功能.
//復制文本
-(void)copyTitle{
NSLog(@"復制功能的實現~");
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];//普通的黏貼板
pasteboard.string = self.testlLabel.text;
}
復制功能實現之后,我們需要實現黏貼功能.
-(void)paste:(id)sender
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
//判斷是否數據
if (nil != pboard.string) {
self.pasteLabel.text = pboard.string;
}
}
到此,復制黏貼的功能就實現了,當然了,這是對一些簡單對象(比如:string,URL,image,color)進行的復制黏貼,如果是復雜對象,改如何進行黏貼復制呢?這時候就需要使用到歸檔和反歸檔把數據變成NSData類型的對象,然后使用 setData:forPasteboardType: 和 dataForPasteboardType: 兩個方法進行數據的存儲和獲取,代碼如下
//存儲數據
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"];
總結: UIPasteboard和UIMenuController兩個類整體使用起來比較簡單,難度一般,如果有什么疑問,請在下面評論,我會及時回復.謝謝您的查看,最后附上測試的Demo.