iOS如何封裝帶復(fù)制功能的UILabel示例代碼
前言
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)為17 label.textAlignment = NSTextAlignmentCenter;//設(shè)置文字位子默認(rèn)靠左 label.numberOfLines = 0;//設(shè)置行數(shù)默認(rèn)為1,當(dāng)為0時(shí)可以就是設(shè)置多行 label.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不在為我們提供長按彈出復(fù)制等操作了, 我們來繼承UILabel自己寫一個(gè)帶復(fù)制功能的UILabel
三、廢話少說,直接擼代碼
#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í)才顯示對就選項(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
四、廢話少說,直接看效果
- (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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- iOS基于 UILabel實(shí)現(xiàn)文字添加描邊功能
- iOS開發(fā)總結(jié)之UILabel常用屬性介紹
- iOS中UILabel設(shè)置居上對齊、居中對齊、居下對齊及文字置頂顯示
- iOS動(dòng)態(tài)調(diào)整UILabel高度的幾種方法
- iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例
- iOS中UILabel實(shí)現(xiàn)長按復(fù)制功能實(shí)例代碼
- IOS 開發(fā)之UILabel 或者 UIButton加下劃線鏈接
- iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度
- iOS兩丫技術(shù)之UILabel性能不夠的解決方法
相關(guān)文章
詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn)
這篇文章主要介紹了詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
IOS實(shí)現(xiàn)簽到特效(散花效果)的實(shí)例代碼
這篇文章主要介紹了IOS實(shí)現(xiàn)簽到特效(散花效果)的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法
這篇文章主要介紹了iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
iOS 實(shí)現(xiàn)類似QQ分組樣式的兩種方式
這篇文章主要介紹了iOS 實(shí)現(xiàn)類似QQ分組樣式的兩種方式,思路很簡單,對模型數(shù)據(jù)操作或則控制界面顯示,需要的朋友可以參考下2017-07-07

