在iOS中下面三個(gè)控件,自身就有復(fù)制-粘貼的功能:
1、UITextView
2、UITextField
3、UIWebView
UIKit framework提供了幾個(gè)類和協(xié)議方便我們在自己的應(yīng)用程序中實(shí)現(xiàn)剪貼板的功能。
1、UIPasteboard:我們可以向其中寫入數(shù)據(jù),也可以讀取數(shù)據(jù)
2、UIMenuController:顯示一個(gè)快捷菜單,用來展示復(fù)制、剪貼、粘貼等選擇的項(xiàng)。
3、UIResponder中的 canPerformAction:withSender:用于控制哪些命令顯示在快捷菜單中。
4、當(dāng)快捷菜單上的命令點(diǎn)擊的時(shí)候,UIResponderStandardEditActions將會被調(diào)用。
下面這些項(xiàng)能被放置到剪貼板中
1、UIPasteboardTypeListString —? 字符串?dāng)?shù)組, 包含kUTTypeUTF8PlainText
2、UIPasteboardTypeListURL —? URL數(shù)組,包含kUTTypeURL
3、UIPasteboardTypeListImage —? 圖形數(shù)組, 包含kUTTypePNG 和kUTTypeJPEG
4、UIPasteboardTypeListColor —? 顏色數(shù)組
剪貼板的類型分為兩種:
系統(tǒng)級:使用UIPasteboardNameGeneral和UIPasteboardNameFind,系統(tǒng)級應(yīng)用程序關(guān)閉,或者卸載的數(shù)據(jù)不會丟失。
應(yīng)用程序級:通過設(shè)置,可以讓數(shù)據(jù)在應(yīng)用程序關(guān)閉之后仍然保存在剪貼板中,但是應(yīng)用程序卸載之后數(shù)據(jù)就會失去。我們可用通過pasteboardWithName:create:來創(chuàng)建。
UILabel
例子如下:
有時(shí)候我們可能需要復(fù)制UILabel上的文本,或者UIImageView的圖片,而UILabel和UIImageView默認(rèn)是不響應(yīng)Touch事件的,也無法復(fù)制,那么我們就需要自己實(shí)現(xiàn)一個(gè)可復(fù)制的UILabel。
新添加一個(gè)類繼承自UILabel:
@interface UICopyLabel : UILabel
@end
#import "UICopyLabel.h"
@implementation UICopyLabel
@end
為了能接收到事件(能成為第一響應(yīng)者),我們需要覆蓋一個(gè)方法:
-(BOOL)canBecomeFirstResponder
{
return YES;
}
還需要針對復(fù)制的操作覆蓋兩個(gè)方法:
// 可以響應(yīng)的方法
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return (action == @selector(copy:));
}
//針對于響應(yīng)方法的實(shí)現(xiàn)
-(void)copy:(id)sender
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
pboard.string = self.text;
}
有了以上三個(gè)方法,我們就能處理copy了,當(dāng)然,在能接收到事件的情況下:
//UILabel默認(rèn)是不接收事件的,我們需要自己添加touch事件
-(void)attachTapHandler
{
self.userInteractionEnabled = YES;? //用戶交互的總開關(guān)
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
touch.numberOfTapsRequired = 2;
[self addGestureRecognizer:touch];
[touch release];
}
//綁定事件
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self attachTapHandler];
}
return self;
}
//同上
-(void)awakeFromNib
{
[super awakeFromNib];
[self attachTapHandler];
}
我們已經(jīng)可以接收到事件了!由于我在上方將tap數(shù)設(shè)為2,所以需要雙擊才能捕獲,
接下來,我們需要處理這個(gè)tap,以便讓菜單欄彈出來:
-(void)handleTap:(UIGestureRecognizer*) recognizer
{
[self becomeFirstResponder];
UIMenuItem *copyLink = [[[UIMenuItemalloc] initWithTitle:@"復(fù)制"
action:@selector(copy:)]autorelease];
[[UIMenuControllersharedMenuController] setMenuItems:[NSArrayarrayWithObjects:copyLink, nil]];
[[UIMenuControllersharedMenuController] setTargetRect:self.frameinView:self.superview];
[[UIMenuControllersharedMenuController] setMenuVisible:YESanimated: YES];
}
這樣一來,一個(gè)可復(fù)制的UILabel就誕生了!它能處理接收點(diǎn)擊、彈出菜單欄、處理copy,這是一個(gè)很普通的可復(fù)制控件。
UIImageView
接下來我們做一個(gè)可復(fù)制的UIImageView,創(chuàng)建一個(gè)新的viewController,放兩個(gè)imageView,默認(rèn)顯示不同的圖:
然后把上面的代碼直接拷過來,改三個(gè)地方:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return (action == @selector(copy:) || action == @selector(paste:));
}
-(void)copy:(id)sender
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
pboard.image = self.image;
}
-(void)paste:(id)sender
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
self.image = pboard.image;
}
UIPasteboard有系統(tǒng)級別和應(yīng)用級別兩種類型,所以不僅可以在應(yīng)用程序內(nèi)通信,還能在應(yīng)用程序間通信,比如我復(fù)制一個(gè)url,然后打開safari,粘貼到地址欄去,而我們可以在應(yīng)用程序間通信、共享數(shù)據(jù)。
在PasteBoardWrite里面點(diǎn)“寫入”后把textField中的文本寫入粘貼板,然后切換到PasteBoardRead的時(shí)候顯示出來。如果我們的粘貼板只想給“自己人”用的話,就不能用系統(tǒng)的通用粘貼板,需要我們自己創(chuàng)建一個(gè):
//需要提供一個(gè)唯一的名字,一般使用倒寫的域名:com.mycompany.myapp.pboard
//后面的參數(shù)表示,如果不存在,是否創(chuàng)建一個(gè)
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
使用這個(gè)粘貼板,我們可以把文本存進(jìn)去,然后在另一個(gè)app里面讀出來,一些常用的類型已經(jīng)被設(shè)置為屬性了:
除此之外,如果是能夠轉(zhuǎn)換成plist的數(shù)據(jù)類型(NSString, NSArray, NSDictionary, NSDate, NSNumber 和 NSURL),我們可以調(diào)用setValue:forPasteboardType:方法去存儲數(shù)據(jù),其他類型只能調(diào)用setData:forPasteboardType:方法(plist數(shù)據(jù)類型也可使用),類似于這樣:
//存儲數(shù)據(jù)
NSDictionary *dict = [NSDictionary dictionaryWithObject:textField.text forKey:@"content"];
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dict];
[pb setData:dictData forPasteboardType:@"myType"];
//獲取就類似于這樣:
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForPasteboardType:@"myType"]];
caption.text = [dict objectForKey:@"content"];
上面提到了一個(gè)PasteboardType, 這是一個(gè)統(tǒng)一類型標(biāo)識符(Uniform Type Identifier? UTI),能幫助app獲取自己能處理的數(shù)據(jù)。比如你只能處理文本的粘貼,那給你一個(gè)UIImage顯然是無用的。你可以使用公用的UTI,也可以使用 任意字符,蘋果建議使用倒寫的域名加上類型名:com.myCompany.myApp.myType。