在iOS中昌跌,可以使用剪貼板實(shí)現(xiàn)應(yīng)用程序之中以及應(yīng)用程序之間實(shí)現(xiàn)數(shù)據(jù)的共享污桦。比如你可以從微信復(fù)制一個(gè)url针肥,粘貼到瀏覽器中查看這個(gè)鏈接的內(nèi)容。而好奇心日報(bào)則是以圖片的形式分享福贞。
好奇心日報(bào)分享功能:復(fù)制文字利用系統(tǒng)分享撩嚼,分享的內(nèi)容不再是簡單的文字,而是一張圖片(類似于封裝好的cell挖帘,樣式固定完丽,只是替換分享的內(nèi)容),且上面有二維碼等拇舀。
在iOS中下面三個(gè)控件逻族,自身就有復(fù)制-粘貼的功能:
1、UITextView
2骄崩、UITextField
3聘鳞、UIWebViewUIKit framework提供了幾個(gè)類和協(xié)議方便我們在自己的應(yīng)用程序中實(shí)現(xiàn)剪貼板的功能。
1要拂、UIPasteboard:我們可以向其中寫入數(shù)據(jù)抠璃,也可以讀取數(shù)據(jù)
2、UIMenuController:顯示一個(gè)快捷菜單脱惰,用來復(fù)制搏嗡、剪貼、粘貼選擇的項(xiàng)拉一。
第一步
- 創(chuàng)建ModelView類采盒,繼承UITextView(因?yàn)榉窒硪窒淼氖菆D片,要寫Share方法蔚润,所不能直接用UITextView)
//在ModelView.m 需要實(shí)現(xiàn)的方法
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
//需要什么動(dòng)作 設(shè)置return為YES
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 YES;
}
else if(action == @selector(selectAll:)){
return YES;
}
else if(action == @selector(_share:)){
return YES;
}
else
{
return [super canPerformAction:action withSender:sender];
}
}
//分享事件 imageView是分享的模板
- (void)_share:(id)sender
{
//獲取粘貼板上分享內(nèi)容
[self CreateImageView];
UIImage *image = [self makeImageWithView:_imageView];
NSArray *postItems=@[image];
//調(diào)用系統(tǒng)分享 把圖片發(fā)送
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
//系統(tǒng)分享需要控制器彈出磅氨,利用modelView找到其所在的視圖控制器
UIViewController *viewC = [self parentController];
[viewC presentViewController:controller animated:YES completion:nil];
}
//復(fù)制事件
- (void)copy:(id)sender {
//復(fù)制字符串到粘貼板上
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.text;
NSLog(@"123");
}
//分享的圖片模板
- (void)CreateImageView
{
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,200,304-10,150)];
_imageView.backgroundColor = [UIColor lightGrayColor];
UITextView *text = [[UITextView alloc]initWithFrame:CGRectMake(20,60,304-70,100)];
text.text = self.text;
text.backgroundColor = [UIColor lightGrayColor];
text.sizeToFit;
[_imageView addSubview:text];
UIImageView *image = [UIImageView new];
image.frame = CGRectMake(10, 10, 30, 30);
image.image = [UIImage imageNamed:@"search.jpg"];
image.layer.cornerRadius = 15;
image.clipsToBounds = YES;
[_imageView addSubview:image];
UITextView *text2 = [[UITextView alloc]initWithFrame:CGRectMake(50,15,200,30)];
text2.text = @"我 分 享 了 一 段 文 字";
text2.backgroundColor = [UIColor lightGrayColor];
text2.sizeToFit;
[_imageView addSubview:text2];
}
//截取view生成image
- (UIImage *)makeImageWithView:(UIView *)view
{
CGSize s = view.bounds.size;
// 下面方法,第一個(gè)參數(shù)表示區(qū)域大小嫡纠。第二個(gè)參數(shù)表示是否是非透明的烦租。如果需要顯示半透明效果,需要傳NO除盏,否則傳YES叉橱。第三個(gè)參數(shù)就是屏幕密度了,關(guān)鍵就是第三個(gè)參數(shù)痴颊。
UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//利用View找到其所在的viewController
-(UIViewController*)parentController{
UIResponder *responder = [self nextResponder];
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)responder;
}
responder = [responder nextResponder];
}
return nil;
}
第二步
- 創(chuàng)建ShareViewController類
//在ShareViewController.m 實(shí)現(xiàn)的方法
#import "ShareViewController.h"
#import "ModelView.h"
@interface ShareViewController (){
UIImageView *_imageView;
}
@end
@implementation ShareViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createTextView];
}
- (void)createTextView
{
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,200,404-10,150)];
_imageView.backgroundColor = [UIColor lightGrayColor];
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView];
//頭像
UIImageView *image = [UIImageView new];
image.frame = CGRectMake(10, 10, 30, 30);
image.image = [UIImage imageNamed:@"search.jpg"];
image.layer.cornerRadius = 15;
image.clipsToBounds = YES;
[_imageView addSubview:image];
//昵稱
UITextView *text2 = [[UITextView alloc]initWithFrame:CGRectMake(45,12,200,30)];
text2.text = @"愛上你等于愛豬";
text2.backgroundColor = [UIColor lightGrayColor];
text2.sizeToFit;
[_imageView addSubview:text2];
//想分享的內(nèi)容
ModelView *model = [[ModelView alloc]initWithFrame:CGRectMake(45, 45, 304, 100)];
model.text = @" 在iOS中赏迟,可以使用剪貼板實(shí)現(xiàn)應(yīng)用程序之中以及應(yīng)用程序之間實(shí)現(xiàn)數(shù)據(jù)的共享屡贺。比如你可以從iPhone QQ復(fù)制一個(gè)url蠢棱,然后粘貼到safari瀏覽器中查看這個(gè)鏈接的內(nèi)容锌杀。";
model.backgroundColor = [UIColor lightGrayColor];
model.sizeToFit;
[_imageView addSubview:model];
}
程序運(yùn)行,運(yùn)行結(jié)果:
屏幕快照 2016-10-15 下午3.10.12.png
我們長按文字泻仙,會(huì)出現(xiàn)菜單
屏幕快照 2016-10-15 下午3.12.47.png
我們既可以選擇需要想分享的文字 點(diǎn)擊copy【此時(shí)粘貼板上就回有我們想分享的文字】糕再,在點(diǎn)擊Share就會(huì)彈出分享 【__一定要真機(jī)測試哦 __】