華山論劍之淺談iOS剪切黏貼(UIPasteboard和UIMenuController)

每個(gè)程序猿都是一名強(qiáng)大的戰(zhàn)士,名曰CV戰(zhàn)士.
菜單功能


序言


如上圖,我們?cè)趹?yīng)用程序中時(shí)常需要有黏貼復(fù)制的功能,像UIWebView,UITextField,UITextView這些類都是自帶黏貼復(fù)制功能的,但是我們先讓某個(gè)控件具有黏貼復(fù)制功能,我們?cè)撛趺崔k呢?那就需要用到兩個(gè)類,一個(gè)是UIPasteboard,另外一個(gè)就是UIMenuController,一個(gè)是剪切板,另外一個(gè)是菜單彈窗.


從UIMenuController到菜單彈窗的完美變身


UIMenuController的用法和UIAlertView的使用方法是類似的,如下圖,我在storyboard的ViewController的控制器中,添加兩個(gè)個(gè)UILabel對(duì)象,然后在ViewController其中添加手勢(shì).

把UILabel拖成屬性,然后代碼如下


#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UILabel *testlLabel;//測(cè)試Label


@property (strong, nonatomic) IBOutlet UILabel *pasteLabel;//黏貼Label


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self loadingTapGR];
}

//添加點(diǎn)擊手勢(shì)
-(void)loadingTapGR{

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(showMenuVC:)];
    

    longPress.minimumPressDuration = 1;
    
    self.testlLabel.userInteractionEnabled = YES;   
    
    [self.testlLabel addGestureRecognizer:longPress];

    UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(showMenuVC:)];
    
    longPress2.minimumPressDuration = 1;
    
    self.pasteLabel.userInteractionEnabled = YES;
    
    [self.pasteLabel addGestureRecognizer:longPress2];

}

//顯示菜單
-(void)showMenuVC:(UILongPressGestureRecognizer *)longPress{

    UIMenuController *menuController = [UIMenuController sharedMenuController];
    
    UIMenuItem * menuItem = [[UIMenuItem alloc] initWithTitle:@"復(fù)制" action:@selector(copyTitle)];
    
    UIMenuItem * pasteItem = [[UIMenuItem alloc] initWithTitle:@"黏貼" action:@selector(paste:)];
    
    NSLog(@"%@",menuController.menuItems);
    
    [menuController setMenuItems:@[menuItem,pasteItem]];
    
    CGPoint location = [longPress locationInView:[longPress view]];
    CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
    [menuController setTargetRect:menuLocation inView:[longPress view]];
    
    [menuController setMenuVisible:YES animated:YES];



}

//復(fù)制文本
-(void)copyTitle{

 
    
}
//黏貼
-(void)paste:(id)sender
{
  

}



@end

代碼是如上實(shí)現(xiàn)了,但是我們點(diǎn)擊Label仍是沒有菜單出現(xiàn),這是為什么呢?因?yàn)槲覀冞€重新定義下面的這個(gè)方法.

//允許成為第一響應(yīng)者
-(BOOL)canBecomeFirstResponder{
    return YES;
}

看一下效果圖,好了菜單完成了,我們接下來要做復(fù)制黏貼的功能了.

效果圖片


從UIPasteboard到復(fù)制黏貼功能的終極進(jìn)化


在使用UIPasteboard之前,我們需要先了解一下剪貼板UIPasteboard的一些基本的知識(shí).

剪貼板類型:

  • 系統(tǒng)級(jí)別:使用UIPasteboardNameGeneral和UIPasteboardNameFind,系統(tǒng)級(jí)應(yīng)用程序關(guān)閉,或者卸載的數(shù)據(jù)不會(huì)丟失。
  • 應(yīng)用程序級(jí):通過設(shè)置,可以讓數(shù)據(jù)在應(yīng)用程序關(guān)閉之后仍然保存在剪貼板中,但是應(yīng)用程序卸載之后數(shù)據(jù)就會(huì)失去。我們可用通過pasteboardWithName:create:來創(chuàng)建。

剪貼板可直接存放的類型

  • 1、UIPasteboardTypeListString — 字符串?dāng)?shù)組 包含kUTTypeUTF8PlainText
  • 2、UIPasteboardTypeListURL — URL數(shù)組,包含kUTTypeURL
  • 3、UIPasteboardTypeListImage — 圖形數(shù)組, 包含kUTTypePNG 和kUTTypeJPEG
  • 4、UIPasteboardTypeListColor — 顏色數(shù)組

下面我們對(duì)上面代碼中的 -(void)copyTitle 方法進(jìn)行進(jìn)一步的完善.使其能有簡單的復(fù)制功能.

//復(fù)制文本
-(void)copyTitle{

    NSLog(@"復(fù)制功能的實(shí)現(xiàn)~");
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];//普通的黏貼板

    pasteboard.string = self.testlLabel.text;
}

復(fù)制功能實(shí)現(xiàn)之后,我們需要實(shí)現(xiàn)黏貼功能.


-(void)paste:(id)sender
{
  
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    
    //判斷是否數(shù)據(jù)
    if (nil != pboard.string) {

            self.pasteLabel.text = pboard.string;
    }
}



到此,復(fù)制黏貼的功能就實(shí)現(xiàn)了,當(dāng)然了,這是對(duì)一些簡單對(duì)象(比如:string,URL,image,color)進(jìn)行的復(fù)制黏貼,如果是復(fù)雜對(duì)象,改如何進(jìn)行黏貼復(fù)制呢?這時(shí)候就需要使用到歸檔和反歸檔把數(shù)據(jù)變成NSData類型的對(duì)象,然后使用 setData:forPasteboardType:dataForPasteboardType: 兩個(gè)方法進(jìn)行數(shù)據(jù)的存儲(chǔ)和獲取,代碼如下

//存儲(chǔ)數(shù)據(jù) 
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];  
NSDictionary *dic = [NSDictionary dictionaryWithObject:self.testlLabel.text forKey:@"saoDong"];    
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dic];    
[pb setData:dictData forPasteboardType:@"myType"];    
  
//獲取就類似于這樣:   
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];    
NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForPasteboardType:@"myType"]];    
self.pasteLabel.text = [dict objectForKey:@"saoDong"];    


總結(jié): UIPasteboard和UIMenuController兩個(gè)類整體使用起來比較簡單,難度一般,如果有什么疑問,請(qǐng)?jiān)谙旅嬖u(píng)論,我會(huì)及時(shí)回復(fù).謝謝您的查看,最后附上測(cè)試的Demo.

-------> UIPasteboard和UIMenuController使用Demo傳送門

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評(píng)論 19 139
  • 用戶可以在一個(gè)app中復(fù)制文本、圖片、或者其他數(shù)據(jù),并粘貼該數(shù)據(jù)到該app的其他位置,或不同的app中。例如,你可...
    raingu24閱讀 4,416評(píng)論 0 1
  • 內(nèi)容來自于 iOS文檔中 About Text Handling in iOS 部分 ios平臺(tái)提供了顯示及編輯文...
    縱橫而樂閱讀 7,131評(píng)論 2 21
  • 留意到如何處理價(jià)值觀不一致問題。首先尊重肯定對(duì)方的敢于表達(dá)自己的想法,這是我們逸才坦誠精神的體現(xiàn)。其次,表達(dá)對(duì)背后...
    sageness閱讀 273評(píng)論 0 1
  • 我們無法預(yù)知未來的路, 只得集中精力走好腳下的每一步, 該發(fā)生的逃避不了, 生活沒那么簡單,你可以憂心,可以憂慮,...
    linguanjie關(guān)節(jié)閱讀 528評(píng)論 0 4

友情鏈接更多精彩內(nèi)容