iOS开发之复制字符串到剪贴板

截图.jpeg

一、主要思路

  • 1.在 View 里贴上scrollView

  • 2.在scrollView里贴上 UITextView,用于上下滑动展示完整数据

二、程序实现

1、在 View 里贴上scrollView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
- ( void )setupUI {
     
     CGFloat marginX = 15;
     CGFloat cellH = 50;
     CGFloat btnW = 70;
     CGFloat btnH = 30;
     
     // 邀请链接
     UIView *linkView2 = [[UIView alloc] init];
     linkView2.backgroundColor = [UIColor whiteColor];
     linkView2.frame = CGRectMake(0, 100, UI_View_Width, cellH);
     [self.view addSubview:linkView2];
     // 邀请链接label
     UILabel *linkLabel2 = [[UILabel alloc] init];
     linkLabel2.backgroundColor = [UIColor clearColor];
     linkLabel2.frame = CGRectMake(marginX, -1, 60, cellH);
     linkLabel2.text = @ "邀请链接" ;
     linkLabel2.font = [UIFont systemFontOfSize:14];
     linkLabel2.textColor = ZLColor(102, 102, 102);
     [linkView2 addSubview:linkLabel2];
     // 复制按钮
     UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     [copyBtn setTitle:@ "复制"  forState:UIControlStateNormal];
     copyBtn.frame = CGRectMake(UI_View_Width - marginX - btnW, (cellH - btnH) * 0.5, btnW, btnH);
     copyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
     [copyBtn addTarget:self action :@selector(copylinkBtnClick) forControlEvents:UIControlEventTouchUpInside];
     copyBtn.backgroundColor = [UIColor whiteColor];
     [copyBtn setTitleColor:ZLColor(108, 187, 82) forState:UIControlStateNormal];
     copyBtn.layer.borderColor = ZLColor(108, 187, 82).CGColor;
     copyBtn.layer.cornerRadius = 3.0;
     copyBtn.layer.borderWidth = 1.0f;
     [linkView2 addSubview:copyBtn];
     
     // 滑动邀请链接
     UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(linkLabel2.frame), 0, UI_View_Width - 60 - btnW - marginX * 2, cellH)];
     scrollView.showsHorizontalScrollIndicator = NO;
     scrollView.showsHorizontalScrollIndicator = NO;
     scrollView.bounces = NO;
     [linkView2 addSubview:scrollView];
     
     UITextView *link = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, UI_View_Width - 60 - btnW - marginX * 2, 50)];
     link.text = @ "wwww.ujfwegertkyluiopafsdfghnlrjkliop[sdfghjklertyui测试测试滚动测试测试滚动测试测试滚动测试测试滚动测试测试滚动" ;
     link.textColor = [UIColor grayColor];
     link.textContainer.maximumNumberOfLines = 1;
     link.scrollEnabled = YES; //是否可以拖动
     link.editable = NO; //禁止编辑
     [scrollView addSubview:link];
     scrollView.contentSize = CGSizeMake(CGRectGetWidth(link.bounds), 50);
     self.link = link;
}


2、在scrollView里贴上 UITextView,用于上下滑动展示完整数据

1
2
3
4
5
6
7
8
9
10
11
12
#pragma mark - 按钮点击事件操作
 
/**
  * 复制链接
  */
- ( void )copylinkBtnClick {
     
     NSLog(@ "复制内容: %@" , self.link.text);
     
     UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
     pasteboard.string = self.link.text;
}
三、压缩文件截图及运行效果

1、压缩文件截图

F8B77280-9FC3-4A92-804B-9E595EB6E6D4.png


2、当显示过多则滚动显示的运行时的截图

运行效果.gif

四、其他补充

界面性问题可以根据自己项目需求调整即可, 具体可参考代码, 项目能够直接运行!

如需看详情版,请点击这里下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值