标签:ios
系统剪贴板的直接调用
其实整个过程非常的简单,我就用我写的一个自定义UILable来说明调用系统剪贴板。
首先,因为苹果只放出来了 UITextView,UITextField,webView三个控件的剪贴板,所以一般控件的剪贴板都被禁用了,因此,我们首先要做的就是把这属性放出来,其实就是实现三个简单的方法,方法如下:
响应方法
- (BOOL)canBecomeFirstResponder{
returnYES;
}
//"反馈"关心的功能,即放出你需要的功能,比如你要放出copy,你就返回YES,否则返回NO;
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action ==@selector(copy:)){
returnYES;
}
else if (action ==@selector(paste:)){
return YES;
}
else if (action ==@selector(cut:)){
returnNO;
}
else if(action ==@selector(select:)){
returnNO;
}
else if (action ==@selector(delete:)){
returnNO;
}
return NO;
}
OK,开放控件的剪贴板功能已经放出,剩下的就是实现了
//UILabel默认是不接收事件的,我们需要自己添加touch事件
-(void)attachTapHandler{
self.userInteractionEnabled =YES; //用户交互的总开关
UITapGestureRecognizer *touch = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleTap:)];
touch.numberOfTapsRequired =1;
[selfaddGestureRecognizer:touch];
[touch release];
}
响应点击事件
-(void)handleTap:(UIGestureRecognizer*) recognizer{
[selfbecomeFirstResponder];
UIMenuController *menu = [UIMenuControllersharedMenuController];
[menu setTargetRect:self.frameinView:self.superview];
[menu setMenuVisible:YESanimated:YES];
}
OK,此处已经可以点击出COPY菜单了,下面就是对你copy和paste的实现了
//针对于copy的实现
-(void)copy:(id)sender{
UIPasteboard *pboard = [UIPasteboardgeneralPasteboard];
pboard.string = self.text;
}
- (void)paste:(id)sender{
self.textAlignment =UITextAlignmentRight;
UIPasteboard *pboard = [UIPasteboardgeneralPasteboard];
self.text = [NSStringstringWithFormat:@"粘贴内容:%@",pboard.string];
NSLog(@"pboard.string : %@",pboard.string);
}
自此,一个可复制的UILabel遍腾空出世了,呵呵,在总结总结 UITableView里面剪贴板的使用。‘
特附上程序图3张。
UITableView上实用剪贴板的两种方法;
一:在自定义cell里面使用
其实,在cell的使用跟在label里面的用法基本一样
1.放方法:
- (BOOL)canBecomeFirstResponder{
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action == @selector(cut:)){
return NO;
}
else if(action == @selector(copy:)){
return YES;
}
else if(action == @selector(paste:)){
return NO;
}
else if(action == @selector(select:)){
return NO;
}
else if(action == @selector(selectAll:)){
return NO;
}
else{
return [super canPerformAction:action withSender:sender];
}
}
然后呢,当然就是 实现copy方法拉。。
- (void)copy:(id)sender {
NSLog(@"copy");
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:[[self textLabel]text]];
}
不过还有最重要的一步,上此方法,它的意思是用来激发copy菜单哦。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[[self delegate] performSelector:@selector(showMenu:)
withObject:self afterDelay:0.9f];
[super setHighlighted:highlighted animated:animated];
}
至于生成菜单,已经很熟悉了,看代码:
//显示菜单
- (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];
}
}
OK,剩下的至于如何实用自定义的cell,大家应该已经很熟练了吧,我就不在一一介绍了,自此,第一种使用剪贴板的方法介绍完毕
二:第二种就更简单了,因为(__MAC_NA,__IPHONE_5_0)以后已经放出了3个方法,非常好用,我直接就上方法拉。
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action == @selector(copy:)) {
return YES;
}
return NO;
}
-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = cell.textLabel.text;
}
}
有上面三个方法,我想,你肯定能轻松实现cell里面剪贴板的使用了。。
咱总结总结自定义剪贴板的使用
其实自定义剪贴板也非常简单,无非是放开响应时间,通过UIMenuController自定义剪贴板,然后就是最关键的实现你所用的copy方法拉。
为了方便实用,我给cell添加了长按事件,看代码---
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[cell addGestureRecognizer:recognizer];
好,事件加上了,剩下的点击就是实现自定义剪贴板了,上代码:
- (void)longPress:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateBegan) {
CopyCell *cell = (CopyCell *)recognizer.view;
[cell 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]];
NSLog(@".....%@",NSStringFromCGRect(cell.frame));
[menu setTargetRect:cell.frame inView:cell.superview];
[menu setMenuVisible:YES animated:YES];
}
}
通过上面这段代码,自定义剪贴板成功搞定,运行,你猛然会发现,操,怎么剪贴板没有出来,我猜你已经想到为什么了,因为- (BOOL)canBecomeFirstResponder{
return YES;
}
这么重要的一句话没有加,OK,下面就是你所要使用的方法了
- (void)flag:(id)sender {
NSLog(@"Cell was flagged");
}
- (void)approve:(id)sender {
NSLog(@"Cell was approved");
}
- (void)deny:(id)sender {
NSLog(@"Cell was denied");
}
好,这就是基本的剪贴板的自定义使用了,为了更直观,上述三篇文章均由下面相似的效果图一张
IOS剪贴版UIPasteboard/UIMenuController
标签:ios
原文地址:http://blog.csdn.net/wangzhaobin/article/details/44960405