iOS UILABEL \UIIMAGE複製
UIKit framework提供了幾個類和協議方便我們在自己的應用程式中實現剪貼簿的功能。
應用程式級:通過設定,可以讓資料在應用程式關閉之後仍然儲存在剪貼簿中,但是應用程式解除安裝之後資料就會失去。我們可用通過pasteboardWithName:create:來建立。
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">@interface UICopyLabel : UILabel @end #import "UICopyLabel.h" @implementation UICopyLabel @end </a>
為了能接收到事件(能成為第一響應者),我們需要覆蓋一個方法:
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">-(BOOL)canBecomeFirstResponder { return YES; } </a>
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">// 可以響應的方法 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { return (action == @selector(copy:)); } </a>
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">//針對於響應方法的實現 -(void)copy:(id)sender { UIPasteboard *pboard = [UIPasteboard generalPasteboard]; pboard.string = self.text; } </a>
有了以上三個方法,我們就能處理copy了,當然,在能接收到事件的情況下:
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">//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]; } </a>
我們已經可以接收到事件了!由於我在上方將tap數設為2,所以需要雙擊才能捕獲,接下來,我們需要處理這個tap,以便讓選單欄彈出來:
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">-(void)handleTap:(UIGestureRecognizer*) recognizer { [self becomeFirstResponder]; UIMenuItem *copyLink = [[[UIMenuItemalloc] initWithTitle:@"複製" action:@selector(copy:)]autorelease]; [[UIMenuControllersharedMenuController] setMenuItems:[NSArrayarrayWithObjects:copyLink, nil]]; [[UIMenuControllersharedMenuController] setTargetRect:self.frameinView:self.superview]; [[UIMenuControllersharedMenuController] setMenuVisible:YESanimated: YES]; } </a>
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">-(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; } </a>
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">//需要提供一個唯一的名字,一般使用倒寫的域名:com.mycompany.myapp.pboard //後面的參數列示,如果不存在,是否建立一個 UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES]; </a>
使用這個貼上板,我們可以把文字存進去,然後在另一個app裡面讀出來,一些常用的型別已經被設定為屬性了:
<a target=_blank href="http://shop111849857.taobao.com/shop/view_shop.htm?spm=a1z0e.1.10010.4.Dadm77">//儲存資料 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"]; </a>
相關文章
- UILabel實現複製貼上功能UI
- [iOS] UIImage和CGImageRefiOSUI
- iOS開發-圖片UIImageiOSUI
- iOS之物件複製iOS物件
- ios開發UI篇--UILabeliOSUI
- iOS 複製/黏貼UIPasteboardiOSUIAST
- iOS UILabel顯示html標籤iOSUIHTML
- iOS筆記之UILabel(富文字)iOS筆記UI
- iOS 複製知識點總結iOS
- ccrendertexture to uiimageUI
- 淺複製和深複製的概念與值複製和指標複製(引用複製)有關 淺複製 “指標複製 深複製 值複製指標
- 原始碼閱讀:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat原始碼WebUIORM
- Java引用複製、淺複製、深複製Java
- iOS-UILabel充分利用NSAttributedStringiOSUI
- ios7 UILabel sizeWithFont方法的最新寫法iOSUI
- iOS開發- UILabel的基本介紹與使用iOSUI
- JS物件複製:深複製和淺複製JS物件
- poi操作excel,複製sheet,複製行,複製單元格,複製styleExcel
- 將UIView轉成UIImage,將UIImage轉成PNG/JPGUIView
- iOS UILabel/UIButton文字設定多個顏色iOSUI
- MySQL 8 複製(三)——延遲複製與部分複製MySql
- 主從複製、雙主複製及半同步複製、以及基於SSL的複製
- MySQL主從複製、半同步複製和主主複製MySql
- JavaScript 淺複製和深複製JavaScript
- mysql複製--主從複製配置MySql
- UIImage 影象處理UI
- OC UIImage基礎UI
- iOS 【如何去除 UILabel 邊緣異常黑線/陰影】iOSUI
- iOS單例ViewController與UIImage物件記憶體優化iOS單例ViewControllerUI物件記憶體優化
- MySQL主從複製、半同步複製和主主複製概述MySql
- (轉)iOS長按textView複製貼上顯示中文iOSTextView
- MySQL 8 複製(一)——非同步複製MySql非同步
- MySQL 8 複製(二)——半同步複製MySql
- go slice深複製和淺複製Go
- MySQL主從複製_複製過濾MySql
- c#淺複製與深複製C#
- js 實現深複製/深複製JS
- python 淺複製、深複製坑Python