如果是在IOS的应用方面,很多时候我们需要封装UILabel,下面这篇文章主要给大家介绍了关于iOS如何封装带复制功能的UILabel的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可
如果是在IOS的应用方面,很多时候我们需要封装UILabel,下面这篇文章主要给大家介绍了关于iOS如何封装带复制功能的UILabel的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
如果是在IOS的应用方面,很多时候我们需要封装UILabel,下面这篇文章主要给大家介绍了关于iOS如何封装带复制功能的UILabel的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
前言
uilabel继承自uiview是ios中使用非常频繁的一个视图控件一般用于显示文字。
一:基本使用
1.创建
|
1
2
|
uilabel *label = [[uilabel alloc]initwithframe:cgrectmake(20, 64, 100, 30)];[self.view addsubview:label]; |
2.属性设置
在ios中你想要使用一个属性一般就直接“.”属性英文名称,或者“set”属性英文名称一般就可以出现
|
1
2
3
4
5
6
7
8
9
10
11
12
|
label.backgroundcolor = [uicolor yellowcolor];//设置背景颜色label.textcolor = [uicolor redcolor];//设置label上文字的颜色label.text = @"我是一个uilabel";//设置label上的文字label.font = [uifont systemfontofsize:15];//设置label上文字的大小 默认为17label.textalignment = nstextalignmentcenter;//设置文字位子默认靠左label.numberoflines = 0;//设置行数默认为1,当为0时可以就是设置多行label.font = [uifont fontwithname:@"arial" size:30];//设置内容字体和字体大小label.highlighted = yes;//label是否高亮//有时偶尔会使用到阴影设置label.shadowcolor = [uicolor bluecolor];//设置阴影颜色label.shadowoffset = cgsizemake(10, 10);//设置阴影的偏移 |
二、在ios中下面三个控件,自身就有复制-粘贴的功能:
1、uitextview
2、uitextfield
3、uiwebview
在ios8 之后, 我们发现uilabel不在为我们提供长按弹出复制等操作了, 我们来继承uilabel自己写一个带复制功能的uilabel
三、废话少说,直接撸代码
|
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
|
#import "copylabel.h"@implementation copylabel- (instancetype)initwithframe:(cgrect)frame { if (self = [super initwithframe:frame]) { [self pressaction]; } return self;}// 初始化设置- (void)pressaction { self.userinteractionenabled = yes; uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(longpressaction:)]; longpress.minimumpressduration = 0.25; [self addgesturerecognizer:longpress];}// 使label能够成为响应事件- (bool)canbecomefirstresponder { return yes;}// 自定义方法时才显示对就选项菜单,即屏蔽系统选项菜单- (bool)canperformaction:(sel)action withsender:(id)sender { if (action == @selector(customcopy:)){ return yes; } return no;}- (void)customcopy:(id)sender { uipasteboard *pasteboard = [uipasteboard generalpasteboard]; pasteboard.string = self.text;}- (void)longpressaction:(uigesturerecognizer *)recognizer { if (recognizer.state == uigesturerecognizerstatebegan) { [self becomefirstresponder]; uimenuitem *copyitem = [[uimenuitem alloc] initwithtitle:@"拷贝" action:@selector(customcopy:)]; uimenucontroller *menucontroller = [uimenucontroller sharedmenucontroller]; menucontroller.menuitems = [nsarray arraywithobjects:copyitem, nil]; [menucontroller settargetrect:self.frame inview:self.superview]; [menucontroller setmenuvisible:yes animated:yes]; }}@end |
四、废话少说,直接看效果
|
1
2
3
4
5
6
7
8
9
10
|
- (void)viewdidload { [super viewdidload]; copylabel *copy = [[copylabel alloc] initwithframe:cgrectmake(0, 100, [uiscreen mainscreen].bounds.size.width,50)]; copy.text = @"清明时节雨纷纷,路上行人欲断魂。"; copy.textalignment = nstextalignmentcenter; copy.backgroundcolor = [uicolor yellowcolor]; copy.textcolor = [uicolor redcolor]; copy.font = [uifont boldsystemfontofsize:16]; [self.view addsubview:copy];} |

五、github地址:
https://github.com/gitwangxiancheng/copylabel.git
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对米米素材网的支持。
原文链接:https://www.jianshu.com/p/c47ea8835c9d
发表评论