iOS开发之详解剪贴板

关于UIMenuController的用法例子

今天终于搞明白了UIMenuController显示的相关内容,把源代码分享给大家!

要正常显示菜单,必须做到以下几点:
1. -(BOOL)canBecomeFirstResponder 必须返回YES

2. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
该函数中,要显示的菜单项(包括系统的菜单项)的方法必须返回YES

3. 在显示菜单前,必须调用:
[self becomeFirstResponder]
成为第一个响应者

4. 为了马上可以正常显示第二个菜单,必须使用:

    [menuController setMenuVisible:NO];
先关闭一下,不然就显示不出来!这是我好不容易才发现的!!!大家鼓励下哦

 

 

 

在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,在这个类的上显示快捷菜单,实现复制功能。

 

 @interface CopyTableViewCell : UITableViewCell {
    id delegate;
}
@property (nonatomic, retain) id delegate;
@end

 

 

 

实现CopyTableViewCell ,实现粘贴:

 


#import "CopyTableViewCell.h"  

@implementation CopyTableViewCell  

@synthesize delegate;  

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {  
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {  
}  
return self;  
}  
- (void)setSelected:(BOOL)ed animated:(BOOL)animated {  
[super setSelected:ed animated:animated];  
}  
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {  
[[self delegate] performSelector:@or(showMenu:)  
withObject:self afterDelay:0.9f];  

[super setHighlighted:highlighted animated:animated];  

}  
- (BOOL)canBecomeFirstResponder  
{  
return YES;  
}  
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
if (action == @or(cut:)){  
return NO;  
}  
else if(action == @or(copy:)){  
return YES;  
}  
else if(action == @or(paste:)){  
return NO;  
}  
else if(action == @or(:)){  
return NO;  
}  
else if(action == @or(All:)){  
return NO;  
}  
else 
{  
return [super canPerformAction:action withSender:sender];  
}  
}  
- (void)copy:(id)sender {  
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];  
[pasteboard setString:[[self textLabel]text]];  
}  
- (void)dealloc {  
[super dealloc];  
}  
@end

 


定义CopyPasteTextController

 


@interface CopyPasteTextController : UIViewController<UITableViewDelegate> {  
//用来标识是否显示快捷菜单  
BOOL menuVisible;  
UITableView *tableView;  
}  

@property (nonatomic, getter=isMenuVisible) BOOL menuVisible;  

@property (nonatomic, retain) IBOutlet UITableView *tableView;  
@end

 

实现CopyPasteTextController :

 


#import "CopyPasteTextController.h"  
#import "CopyTableViewCell.h" 

@implementation CopyPasteTextController  
@synthesize menuVisible,tableView;  
- (void)viewDidLoad {  
[super viewDidLoad];  
[self setTitle:@"文字复制粘贴"];  
//点击这个按钮将剪贴板的内容粘贴到title上  
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]  
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  
target:self  
action:@or(readFromPasteboard:)]  
autorelease];  
[[self navigationItem] setRightBarButtonItem:addButton];  
}  


// Customize the number of sections in the table view.  
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
{  
return 1;  
}  

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
{  
return 9;  
}  

// Customize the appearance of table view cells.  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
static NSString *CellIdentifier =@"Cell";  
CopyTableViewCell *cell = (CopyTableViewCell *)[tableView  
dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil)  
{  
cell = [[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
[cell setDelegate:self];  
}  

// Configure the cell.  
NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]];  
[[cell textLabel] setText:text];  
return cell;  
}  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
{  
if([self isMenuVisible])  
{  
return;  
}  
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES  
animated:YES];  
}  
//显示菜单  
- (void)showMenu:(id)cell {  
if ([cell isHighlighted]) {  
[cell becomeFirstResponder];  

UIMenuController * menu = [UIMenuController sharedMenuController];  
[menu setTargetRect: [cell frame] inView: [self view]];  
[menu setMenuVisible: YES animated: YES];  
}  
}  
- (void)readFromPasteboard:(id)sender {  
[self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",  
[[UIPasteboard generalPasteboard] string]]];  
}  

- (void)didReceiveMemoryWarning  
{  
// Releases the view if it doesn""t have a superview.  
[super didReceiveMemoryWarning];  

// Relinquish ownership any cached data, images, etc that aren""t in use.  
}  

- (void)viewDidUnload  
{  
[super viewDidUnload];  
[self.tableView release];  

// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.  
// For example: self.myOutlet = nil;  

 

结果:

 

复制一行数据:

 

iOS开辟之详解剪贴板

 

 

 

点击右上角的按钮粘贴,将数据显示在title上:

 

iOS开辟之详解剪贴板

 



 

二、图片复制粘贴

 

下面经由过程一个例子,将图片复制和剪贴到别的一个UIImageView中心。

 

1、在界面上放置两个uiimageview,一个是图片的数据源,一个是将图片粘贴到的处所。CopyPasteImageViewController 代码如下:

 


@interface CopyPasteImageViewController : UIViewController {  
UIImageView *imageView;  
UIImageView *pasteView;  
UIImageView *edView;  
}  
@property (nonatomic, retain) IBOutlet UIImageView *imageView;  
@property (nonatomic, retain) IBOutlet UIImageView *pasteView;  
@property (nonatomic, retain) UIImageView *edView;  
- (void)placeImageOnPasteboard:(id)view;  
@end

 

2、当触摸图片的时辰我们显示快捷菜单:

 


- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {  
NSSet *copyTouches = [event touchesForView:imageView];  
NSSet *pasteTouches = [event touchesForView:pasteView];  

[self becomeFirstResponder];  
if ([copyTouches count] > 0) {  
[self performSelector:@or(showMenu:)  
withObject:imageView afterDelay:0.9f];  
}  
else  if([pasteTouches count] > 0) {  
[self performSelector:@or(showMenu:)  
withObject:pasteView afterDelay:0.9f];  
}  
[super touchesBegan:touches withEvent:event];  
}  

- (void)showMenu:(id)view {  
[self setSelectedView:view];  

UIMenuController * menu = [UIMenuController sharedMenuController];  
[menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view];  
[menu setMenuVisible: YES animated: YES];  

 

这里的快捷菜单,显示三个菜单项:剪贴、粘贴、复制:

 


- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
if (action == @or(cut:)) {  
return ([self edView] == imageView) ? YES : NO;  
} else if (action == @or(copy:)) {  
return ([self edView] == imageView) ? YES : NO;  
} else if (action == @or(paste:)) {  
return ([self edView] == pasteView) ? YES : NO;  
} else if (action == @or(:)) {  
return NO;  
} else if (action == @or(All:)) {  
return NO;  
} else {  
return [super canPerformAction:action withSender:sender];  
}  
}  
- (void)cut:(id)sender {  
[self copy:sender];  
[imageView setHidden:YES];  
}  
- (void)copy:(id)sender {  
[self placeImageOnPasteboard:[self imageView]];  
}  
- (void)paste:(id)sender {  
UIPasteboard *appPasteBoard =  
[UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES];  
NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];  
pasteView.image = [UIImage imageWithData:data];  

 

结果:

 

1、点击图片,显示菜单按钮。

 

iOS开辟之详解剪贴板

 

2、点击复制,将数据复制到剪贴板上:

 

iOS开辟之详解剪贴板

 

3、点击粘贴,将数据粘贴到uiimageview上。

 

iOS开辟之详解剪贴板

 

 

 

原文链接:http://blog.csdn.net/zhuqilin0/article/details/6661044

 

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *inputTF;

- (IBAction)cutAction:(UIButton *)sender;

- (IBAction)copyAction:(UIButton *)sender;

- (IBAction)pasteAction:(UIButton *)sender;

- (IBAction)mianban:(UIButton *)sender;

 

@property (weak, nonatomic) IBOutlet UILabel *textLabel;

 

 

@end

 

@implementation ViewController

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.la

    

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 100)];

    [self.view addSubview:label];

    label.text = @"123456789";

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (IBAction)cutAction:(UIButton *)sender {

    

    

    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

    pasteBoard.string = self.inputTF.text;

    self.inputTF.text = @"";

}

 

- (IBAction)copyAction:(UIButton *)sender {

    

    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

 

    pasteBoard.string = self.inputTF.text;

    

}

 

- (IBAction)pasteAction:(UIButton *)sender {

    

    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

    self.textLabel.text = pasteBoard.string;

    

}

 

- (IBAction)mianban:(UIButton *)sender {

    

    UIMenuController * menu = [UIMenuController sharedMenuController];

    

    [menu setTargetRect:CGRectMake(10, 100, 50, 100) inView: [self view]];

    [menu setMenuVisible:NO animated: YES];

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

 

    [self.view endEditing:YES];

}

@end

 

 

转载于:https://www.cnblogs.com/MrXHong/p/5420744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值