国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS如何封裝帶復(fù)制功能的UILabel示例代碼

2020-07-26 02:24:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

UILabel繼承自UIView是iOS中使用非常頻繁的一個(gè)視圖控件一般用于顯示文字。

一:基本使用

1.創(chuàng)建

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 64, 100, 30)];[self.view addSubview:label];

2.屬性設(shè)置

在iOS中你想要使用一個(gè)屬性一般就直接“.”屬性英文名稱,或者“set”屬性英文名稱一般就可以出現(xiàn)

label.backgroundColor = [UIColor yellowColor];//設(shè)置背景顏色label.textColor = [UIColor redColor];//設(shè)置Label上文字的顏色label.text = @"我是一個(gè)UILabel";//設(shè)置Label上的文字label.font = [UIFont systemFontOfSize:15];//設(shè)置Label上文字的大小 默認(rèn)為17label.textAlignment = NSTextAlignmentCenter;//設(shè)置文字位子默認(rèn)靠左label.numberOfLines = 0;//設(shè)置行數(shù)默認(rèn)為1,當(dāng)為0時(shí)可以就是設(shè)置多行l(wèi)abel.font = [UIFont fontWithName:@"Arial" size:30];//設(shè)置內(nèi)容字體和字體大小label.highlighted = YES;//Label是否高亮//有時(shí)偶爾會(huì)使用到陰影設(shè)置label.shadowColor = [UIColor blueColor];//設(shè)置陰影顏色label.shadowOffset = CGSizeMake(10, 10);//設(shè)置陰影的偏移

二、在iOS中下面三個(gè)控件,自身就有復(fù)制-粘貼的功能:

1、UITextView

2、UITextField

3、UIWebView

在iOS8 之后, 我們發(fā)現(xiàn)UILabel不在為我們提供長(zhǎng)按彈出復(fù)制等操作了, 我們來(lái)繼承UILabel自己寫(xiě)一個(gè)帶復(fù)制功能的UILabel

三、廢話少說(shuō),直接擼代碼

#import "CopyLabel.h"@implementation CopyLabel- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self pressAction]; } return self;}// 初始化設(shè)置- (void)pressAction { self.userInteractionEnabled = YES; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; longPress.minimumPressDuration = 0.25; [self addGestureRecognizer:longPress];}// 使label能夠成為響應(yīng)事件- (BOOL)canBecomeFirstResponder {  return YES;}// 自定義方法時(shí)才顯示對(duì)就選項(xiàng)菜單,即屏蔽系統(tǒng)選項(xiàng)菜單- (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

四、廢話少說(shuō),直接看效果

- (void)viewDidLoad { [super viewDidLoad]; CopyLabel *copy = [[CopyLabel alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width,50)]; copy.text = @"清明時(shí)節(jié)雨紛紛,路上行人欲斷魂。"; 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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江油市| 白银市| 徐汇区| 汝南县| 扶风县| 孝昌县| 偏关县| 定结县| 徐水县| 四川省| 临澧县| 永登县| 昭苏县| 黄平县| 南部县| 嘉义县| 和政县| 临潭县| 政和县| 溧阳市| 天祝| 什邡市| 巩留县| 万州区| 九龙县| 新乡县| 堆龙德庆县| 博白县| 辽宁省| 邵东县| 绥化市| 抚远县| 丹凤县| 广饶县| 涟水县| 德江县| 德江县| 化德县| 龙门县| 通道| 顺义区|