ios开发读取剪切板的内容_iOS开发之详解剪贴板
在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:來創建。
了解這些之后,下面通過一系列的例子來說明如何在應用程序中使用剪貼板。
例子:
一、復制剪貼文本。
下面通過一個例子,可以在tableview上顯示一個快捷菜單,上面只有復制按鈕,復制tableview上的數據之后,然后粘貼到title上。
定義一個單元格類CopyTableViewCell,在這個類的上顯示快捷菜單,實現復制功能。
1 @interface CopyTableViewCell : UITableViewCell {2 id delegate;3 }4 @property (nonatomic, retain) id delegate;5 @end
View Code
實現CopyTableViewCell ,實現粘貼:
1 #import "CopyTableViewCell.h"
2
3 @implementation CopyTableViewCell4
5 @synthesize delegate;6
7 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {8 if ((self =[super initWithStyle:style reuseIdentifier:reuseIdentifier])) {9 }10 returnself;11 }12 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {13 [super setSelected:selected animated:animated];14 }15 - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {16 [[self delegate] performSelector:@selector(showMenu:)17 withObject:self afterDelay:0.9f];18
19 [super setHighlighted:highlighted animated:animated];20
21 }22 -(BOOL)canBecomeFirstResponder23 {24 returnYES;25 }26 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{27 if (action ==@selector(cut:)){28 returnNO;29 }30 else if(action ==@selector(copy:)){31 returnYES;32 }33 else if(action ==@selector(paste:)){34 returnNO;35 }36 else if(action == @selector(select:)){37 returnNO;38 }39 else if(action ==@selector(selectAll:)){40 returnNO;41 }42 else
43 {44 return[super canPerformAction:action withSender:sender];45 }46 }47 - (void)copy:(id)sender {48 UIPasteboard *pasteboard =[UIPasteboard generalPasteboard];49 [pasteboard setString:[[self textLabel]text]];50 }51 - (void)dealloc {52 [super dealloc];53 }54 @end
View Code
定義CopyPasteTextController
1 @interface CopyPasteTextController : UIViewController{2 //用來標識是否顯示快捷菜單
3 BOOL menuVisible;4 UITableView *tableView;5 }6
7 @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;8
9 @property (nonatomic, retain) IBOutlet UITableView *tableView;10 @end
View Code
實現CopyPasteTextController :
1 #import "CopyPasteTextController.h"
2 #import "CopyTableViewCell.h"
3
4 @implementation CopyPasteTextController5 @synthesize menuVisible,tableView;6 - (void)viewDidLoad {7 [super viewDidLoad];8 [self setTitle:@"文字復制粘貼"];9 //點擊這個按鈕將剪貼板的內容粘貼到title上
10 UIBarButtonItem *addButton =[[[UIBarButtonItem alloc]11 initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh12 target:self13 action:@selector(readFromPasteboard:)]14 autorelease];15 [[self navigationItem] setRightBarButtonItem:addButton];16 }17
18
19 //Customize the number of sections in the table view.
20 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView21 {22 return 1;23 }24
25 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section26 {27 return 9;28 }29
30 //Customize the appearance of table view cells.
31 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath32 {33 static NSString *CellIdentifier =@"Cell";34 CopyTableViewCell *cell = (CopyTableViewCell *)[tableView35 dequeueReusableCellWithIdentifier:CellIdentifier];36 if (cell ==nil)37 {38 cell =[[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];39 [cell setDelegate:self];40 }41
42 //Configure the cell.
43 NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]];44 [[cell textLabel] setText:text];45 returncell;46 }47
48 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath49 {50 if([self isMenuVisible])51 {52 return;53 }54 [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES55 animated:YES];56 }57 //顯示菜單
58 - (void)showMenu:(id)cell {59 if([cell isHighlighted]) {60 [cell becomeFirstResponder];61
62 UIMenuController * menu =[UIMenuController sharedMenuController];63 [menu setTargetRect: [cell frame] inView: [self view]];64 [menu setMenuVisible: YES animated: YES];65 }66 }67 - (void)readFromPasteboard:(id)sender {68 [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",69 [[UIPasteboard generalPasteboard] string]]];70 }71
72 - (void)didReceiveMemoryWarning73 {74 //Releases the view if it doesn't have a superview.
75 [super didReceiveMemoryWarning];76
77 //Relinquish ownership any cached data, images, etc that aren't in use.
78 }79
80 - (void)viewDidUnload81 {82 [super viewDidUnload];83 [self.tableView release];84
85 //Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.86 //For example: self.myOutlet = nil;
87 }
View Code
效果:
復制一行數據:
點擊右上角的按鈕粘貼,將數據顯示在title上:
二、圖片復制粘貼
下面通過一個例子,將圖片復制和剪貼到另外一個UIImageView中間。
1、在界面上放置兩個uiimageview,一個是圖片的數據源,一個是將圖片粘貼到的地方。CopyPasteImageViewController 代碼如下:
1 @interface CopyPasteImageViewController : UIViewController {2 UIImageView *imageView;3 UIImageView *pasteView;4 UIImageView *selectedView;5 }6 @property (nonatomic, retain) IBOutlet UIImageView *imageView;7 @property (nonatomic, retain) IBOutlet UIImageView *pasteView;8 @property (nonatomic, retain) UIImageView *selectedView;9 - (void)placeImageOnPasteboard:(id)view;10 @end
View Code
2、當觸摸圖片的時候我們顯示快捷菜單:
1 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{2 NSSet *copyTouches = [eventtouchesForView:imageView];3 NSSet *pasteTouches = [eventtouchesForView:pasteView];4
5 [self becomeFirstResponder];6 if ([copyTouches count] > 0) {7 [self performSelector:@selector(showMenu:)8 withObject:imageView afterDelay:0.9f];9 }10 else if([pasteTouches count] > 0) {11 [self performSelector:@selector(showMenu:)12 withObject:pasteView afterDelay:0.9f];13 }14 [super touchesBegan:touches withEvent:event];15 }16
17 - (void)showMenu:(id)view {18 [self setSelectedView:view];19
20 UIMenuController * menu =[UIMenuController sharedMenuController];21 [menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view];22 [menu setMenuVisible: YES animated: YES];23 }
View Code
這里的快捷菜單,顯示三個菜單項:剪貼、粘貼、復制:
1 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{2 if (action ==@selector(cut:)) {3 return ([self selectedView] == imageView) ?YES : NO;4 } else if (action ==@selector(copy:)) {5 return ([self selectedView] == imageView) ?YES : NO;6 } else if (action ==@selector(paste:)) {7 return ([self selectedView] == pasteView) ?YES : NO;8 } else if (action == @selector(select:)) {9 returnNO;10 } else if (action ==@selector(selectAll:)) {11 returnNO;12 } else{13 return[super canPerformAction:action withSender:sender];14 }15 }16 - (void)cut:(id)sender {17 [self copy:sender];18 [imageView setHidden:YES];19 }20 - (void)copy:(id)sender {21 [self placeImageOnPasteboard:[self imageView]];22 }23 - (void)paste:(id)sender {24 UIPasteboard *appPasteBoard =
25 [UIPasteboard pasteboardWithName:@"CopyPasteImage"create:YES];26 NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];27 pasteView.image =[UIImage imageWithData:data];28 }
View Code
效果:
1、點擊圖片,顯示菜單按鈕。
2、點擊復制,將數據復制到剪貼板上:
3、點擊粘貼,將數據粘貼到uiimageview上。
總結:
本文詳解了iOS系統應用程序中如何使用剪貼板。
轉自:http://blog.csdn.net/zhuqilin0/article/details/6661044
總結
以上是生活随笔為你收集整理的ios开发读取剪切板的内容_iOS开发之详解剪贴板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java集合类和数组之间的相互转换
- 下一篇: 映射表跟业务表的区别_方正飞鸿中间件开发