標籤:
在iOS中,可以使用剪貼簿實現應用程式之中以及應用程式之間實現資料的共用。比如你可以從iPhone QQ複製一個url,然後粘貼到safari瀏覽器中查看這個連結的內容。
一、在iOS中下面三個控制項,自身就有複製-粘貼的功能:
1、UITextView
2、UITextField
3、UIWebView
二、UIKit framework提供了幾個類和協議方便我們在自己的應用程式中實現剪貼簿的功能。
1、UIPasteboard:我們可以向其中寫入資料,也可以讀取資料
2、UIMenuController:顯示一個捷徑功能表,用來複製、剪貼、粘貼選擇的項。
3、UIResponder中的 canPerformAction:withSender:用於控制哪些命令顯示在捷徑功能表中。
4、當捷徑功能表上的命令點擊的時候,UIResponderStandardEditActions將會被調用。
三、下面這些項能被放置到剪貼簿中
1、UIPasteboardTypeListString — 字串數組, 包含kUTTypeUTF8PlainText
2、UIPasteboardTypeListURL — URL數組,包含kUTTypeURL
3、UIPasteboardTypeListImage — 圖形數組, 包含kUTTypePNG 和kUTTypeJPEG
4、UIPasteboardTypeListColor — 顏色數組
四、剪貼簿的類型分為兩種:
系統級:使用UIPasteboardNameGeneral和UIPasteboardNameFind建立,系統級的剪貼簿,當應用程式關閉,或者卸載時,資料都不會丟失。
應用程式級:通過設定,可以讓資料在應用程式關閉之後仍然儲存在剪貼簿中,但是應用程式卸載之後資料就會失去。我們可用通過pasteboardWithName:create:來建立。
UIMenuController的使用,對UILabel拷貝以及定製菜單
1. Menu所處的View必須實現 – (BOOL)canBecomeFirstResponder, 且返回YES
2. Menu所處的View必須實現 – (BOOL)canPerformAction:withSender, 並根據需求返回YES或NO
3. 使Menu所處的View成為First Responder (becomeFirstResponder)
4. 定位Menu (- setTargetRect:inView:)
5. 展示Menu (- setMenuVisible:animated:)
- @implementation UICopyLabel
-
- // default is NO
- - (BOOL)canBecomeFirstResponder{
- return YES;
- }
-
- //"反饋"關心的功能
- -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
- return (action == @selector(copy:));
- }
- //針對於copy的實現
- -(void)copy:(id)sender{
- UIPasteboard *pboard = [UIPasteboard generalPasteboard];
- pboard.string = self.text;
- }
-
- //UILabel預設是不接收事件的,我們需要自己添加touch事件
- -(void)attachTapHandler{
- self.userInteractionEnabled = YES; //使用者互動的總開關
- 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];
- }
-
- -(void)handleTap:(UIGestureRecognizer*) recognizer{
- [self becomeFirstResponder];
- UIMenuController *menu = [UIMenuController sharedMenuController];
- [menu setTargetRect:self.frame inView:self.superview];
- [menu setMenuVisible:YES animated:YES];
- }
-
-
- @end
在view裡添加一個UICopyLabel
現在可以使用UICopyLabel實現雙擊來對label的內容copy了
在你的view中
UICopyLabel *display = [[UICopyLabelalloc]initWithFrame:CGRectMake(30,100,250,30)];
awakeFromNib
在使用IB的時候才會涉及到此方法的使用,當.nib檔案被載入的時候,會發送一個awakeFromNib的訊息到.nib檔案中的每個對象,每個對象都可以定義自己的awakeFromNib函數來響應這個訊息,執行一些必要的操作。
看例子:
建立一個viewController with XIB
定義一個UIView的子類
開啟xib,並把View的類型指定為上一步驟定義的子類
然後在TestView.m中加入 awakeFromNib方法,運行程式發現此方法被調用了!!!
下面我們來定製菜單
在attachTapHandler中添加長按壓手勢
- -(void)attachTapHandler{
- self.userInteractionEnabled = YES; //使用者互動的總開關
- //雙擊
- UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
- touch.numberOfTapsRequired = 2;
- [self addGestureRecognizer:touch];
- [touch release];
-
- //長按壓
- UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
- press.minimumPressDuration = 1.0;
- [self addGestureRecognizer:press];
- [press release];
-
- }
添加方法longPress
- - (void)longPress:(UILongPressGestureRecognizer *)recognizer {
- if (recognizer.state == UIGestureRecognizerStateBegan) {
- // TSTableViewCell *cell = (TSTableViewCell *)recognizer.view;
- [self becomeFirstResponder];
- UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag" action:@selector(flag:)];
- UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve" action:@selector(approve:)];
- UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny" action:@selector(deny:)];
-
- UIMenuController *menu = [UIMenuController sharedMenuController];
- [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
- [menu setTargetRect:self.frame inView:self.superview];
- [menu setMenuVisible:YES animated:YES];
- NSLog(@"menuItems:%@",menu.menuItems);
- }
- }
-
- - (void)flag:(id)sender {
- NSLog(@"Cell was flagged");
- }
-
- - (void)approve:(id)sender {
- NSLog(@"Cell was approved");
- }
-
- - (void)deny:(id)sender {
- NSLog(@"Cell was denied");
- }
修改canPerformAction
[cpp] view plaincopyprint?
- -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
- // return (action == @selector(copy:));
- if (action == @selector(copy:)||action == @selector(flag:)||action == @selector(approve:)||action == @selector(deny:)) {
- return YES;
- }
- }
ok。。。效果
ios 複製黏貼板的使用