ios 複製黏貼板的使用

來源:互聯網
上載者:User

標籤:

在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:)

 

 

  1. @implementation UICopyLabel  
  2.   
  3. // default is NO  
  4. - (BOOL)canBecomeFirstResponder{  
  5.     return YES;  
  6. }  
  7.   
  8. //"反饋"關心的功能    
  9. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{    
  10.     return (action == @selector(copy:));    
  11. }    
  12. //針對於copy的實現    
  13. -(void)copy:(id)sender{    
  14.     UIPasteboard *pboard = [UIPasteboard generalPasteboard];    
  15.     pboard.string = self.text;    
  16. }   
  17.   
  18. //UILabel預設是不接收事件的,我們需要自己添加touch事件    
  19. -(void)attachTapHandler{    
  20.     self.userInteractionEnabled = YES;  //使用者互動的總開關    
  21.     UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];    
  22.     touch.numberOfTapsRequired = 2;    
  23.     [self addGestureRecognizer:touch];    
  24.     [touch release];    
  25. }    
  26. //綁定事件    
  27. - (id)initWithFrame:(CGRect)frame    
  28. {    
  29.     self = [super initWithFrame:frame];    
  30.     if (self) {    
  31.         [self attachTapHandler];    
  32.     }    
  33.     return self;    
  34. }    
  35. //同上    
  36. -(void)awakeFromNib{    
  37.     [super awakeFromNib];    
  38.     [self attachTapHandler];    
  39. }  
  40.   
  41. -(void)handleTap:(UIGestureRecognizer*) recognizer{    
  42.     [self becomeFirstResponder];    
  43.     UIMenuController *menu = [UIMenuController sharedMenuController];    
  44.     [menu setTargetRect:self.frame inView:self.superview];    
  45.     [menu setMenuVisible:YES animated:YES];    
  46. }   
  47.   
  48.   
  49. @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中添加長按壓手勢

 

 

 
  1. -(void)attachTapHandler{    
  2.     self.userInteractionEnabled = YES;  //使用者互動的總開關    
  3.     //雙擊  
  4.     UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];    
  5.     touch.numberOfTapsRequired = 2;    
  6.     [self addGestureRecognizer:touch];   
  7.      [touch release];   
  8.       
  9.     //長按壓  
  10.     UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];  
  11.     press.minimumPressDuration = 1.0;  
  12.     [self addGestureRecognizer:press];  
  13.     [press release];  
  14.       
  15. }   

 

添加方法longPress

 

 
  1. - (void)longPress:(UILongPressGestureRecognizer *)recognizer {  
  2. if (recognizer.state == UIGestureRecognizerStateBegan) {  
  3. //   TSTableViewCell *cell = (TSTableViewCell *)recognizer.view;  
  4.         [self becomeFirstResponder];  
  5.         UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag" action:@selector(flag:)];  
  6.         UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve" action:@selector(approve:)];  
  7. UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny" action:@selector(deny:)];  
  8.           
  9.         UIMenuController *menu = [UIMenuController sharedMenuController];  
  10. [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];  
  11. [menu setTargetRect:self.frame inView:self.superview];  
  12.         [menu setMenuVisible:YES animated:YES];  
  13.          NSLog(@"menuItems:%@",menu.menuItems);  
  14. }  
  15. }  
  16.   
  17. - (void)flag:(id)sender {  
  18. NSLog(@"Cell was flagged");  
  19. }  
  20.   
  21. - (void)approve:(id)sender {  
  22. NSLog(@"Cell was approved");  
  23. }  
  24.   
  25. - (void)deny:(id)sender {  
  26. NSLog(@"Cell was denied");  
  27. }  

 

修改canPerformAction

 

 

[cpp] view plaincopyprint? 
  1. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{    
  2. //    return (action == @selector(copy:));    
  3.     if (action == @selector(copy:)||action == @selector(flag:)||action == @selector(approve:)||action == @selector(deny:)) {  
  4.         return YES;  
  5.     }  
  6. }  

 

ok。。。效果

 

 

 

ios 複製黏貼板的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.