标签:大小 平安 工作效率 字体 black extc bounds uila 重复
在IOS开发的过程中,UILabel是很常用的一个控件,同时也是大量使用的一个控件。创建一个UILabel一般需要五六句代码,如果我们需要创建几十个UILabel,就意味着我们要写五六十句代码,其实很多代码是重复的,我们可以把类似的代码写到一个公共的方法中,以提高工作效率和降低代码重复。官方提供UILabel的一些属性有很大的局限性,有些在项目中开发中需要用到的一些拓展性的属性,根据个人经验,也顺便一起总结在这里。
一、创建UILabel公共的方法
1、头文件中声明方法如下:
|
1
2
3
4
5
|
+ (UILabel *)commonLabelWithFrame:(CGRect)frame text:(NSString*)text color:(UIColor*)color font:(UIFont*)font textAlignment:(NSTextAlignment)textAlignment; |
2、源文件中实现该方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
+ (UILabel *)commonLabelWithFrame:(CGRect)frame text:(NSString*)text color:(UIColor*)color font:(UIFont*)font textAlignment:(NSTextAlignment)textAlignment{ UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.text = text; label.textColor = color; label.font = font; label.textAlignment = textAlignment; label.backgroundColor = [UIColor clearColor]; return label;} |
二、动态设置UILabel高度
1、头文件申明方法如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 创建一个动态高度的UILabel * * @param pointX Label的横坐标 * @param pointY Label的纵坐标 * @param width Label的宽度 * @param strContent 内容 * @param color 字体颜色 * @param font 字体大小 * @param textAlignmeng 对齐方式 * * @return 返回一个UILabel */+ (UILabel *)dynamicHeightLabelWithPointX:(CGFloat)pointX pointY:(CGFloat)pointY width:(CGFloat)width strContent:(NSString *)strContent color:(UIColor *)color font:(UIFont *)font textAlignmeng:(NSTextAlignment)textAlignmeng; |
2、源文件中实现该方法:
|
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
|
//动态设置Label的高度+ (UILabel *)dynamicHeightLabelWithPointX:(CGFloat)pointX pointY:(CGFloat)pointY width:(CGFloat)width strContent:(NSString *)strContent color:(UIColor *)color font:(UIFont *)font textAlignmeng:(NSTextAlignment)textAlignmeng{ NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy}; CGSize labelSize = [strContent boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(pointX, pointY, width, labelSize.height)]; [myLabel setNumberOfLines:0]; myLabel.text = strContent; myLabel.font = font; myLabel.textColor = color; return myLabel;} |
3、测试结果:
|
1
2
3
4
5
6
7
8
9
10
11
|
- (void)viewDidLoad { [super viewDidLoad]; NSString *str = @6月初,华润华发联合体以87.95亿元拿下上海闸北地块,地块楼面价38061元/平方米,刷新了其自身于3月创下的上海总价“地王”纪录。同日,招商平安联合体则以高达2.3万元/平方米的楼面价,竞得宝山大场镇地块,创出近90%的高溢价率。不仅是一线市场,杭州、苏州等二线市场也在6月初集中推地。杭州西溪湿地旁低密度住宅地块楼面价9975元/平方米,溢价率33%,成为2014年春节以来杭州溢价率最高的住宅用地。; UILabel *label = [LTLabel dynamicHeightLabelWithPointX:5 pointY:20 width:self.view.frame.size.width-10 strContent:str color:[UIColor blackColor] font:[UIFont systemFontOfSize:20.0] textAlignmeng:NSTextAlignmentLeft]; label.backgroundColor = [UIColor groupTableViewBackgroundColor]; [self.view addSubview:label]; } |
(1)字体大小为15号,与边距间隔为5,测试结果如下:

(2)字体大小为20号,于边距间隔为5,测试结果如下:

(3)字体大小为20号,于边距间隔为50,测试结果如下:

(4)字体大小为20号,于边距间隔为5,增加文本内容,测试结果如下:

三、设置UILabel的对齐方式
对于官方已经提供UILabel的一些对齐方式,在这里就不做说明了,这里主要补充官方没有提供的对齐方式。主要提供了三种常用的对齐方式:垂直顶端对齐、顶端居中对齐、顶端靠右对齐。
1、头文件申明方法如下:
|
1
2
3
4
5
6
7
8
9
|
@interface DpLabel : UILabeltypedef enum{ VerticalAlignmentTop = 0, //default 垂直顶端对齐 VerticalAlignmentMidele, //顶端居中对齐 VerticalAlignmentBottom, //顶端靠右对齐}VerticalAlignment;@property (nonatomic, assign) VerticalAlignment verticalAlignment; |
2、源文件实现该方法:
|
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#import DpLabel.h@implementation DpLabel@synthesize verticalAlignment;- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code verticalAlignment = VerticalAlignmentTop; } return self;}- (VerticalAlignment)verticalAlignment{ return verticalAlignment;}- (void)setVerticalAlignment:(VerticalAlignment)align{ verticalAlignment = align; [self setNeedsDisplay];}- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{ CGRect rc = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; switch (verticalAlignment) { case VerticalAlignmentTop: rc.origin.y = bounds.origin.y; break; case VerticalAlignmentBottom: rc.origin.y = bounds.origin.y + bounds.size.height - rc.size.height; break; default: rc.origin.y = bounds.origin.y + (bounds.size.height - rc.size.height)/2; break; } return rc;}- (void)drawTextInRect:(CGRect)rect{ CGRect rc = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines]; [super drawTextInRect:rc];}//调整文本中的行距的方法/*使用方法 * *text参数 :文本内容 * *height参数:行距 * *name 参数:你使用的 UIlable 对象 */- (void) getlable_height :(NSString *) text uiheight:(NSInteger) height uilable:(UILabel*) name{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:height];//调整行间距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])]; name.attributedText = attributedString; }@end |
3、测试结果
|
1
2
3
4
5
6
7
8
9
|
<span style="font-size:18px;">- (void)viewDidLoad { [super viewDidLoad]; DpLabel *label = [[DpLabel alloc] initWithFrame:CGRectMake(20, 120, self.view.frame.size.width-40, 50)]; label.text = @测试对齐方式; label.textAlignment = VerticalAlignmentTop; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label];}</span> |
(1)测试垂直顶端对齐方式,测试结果如下:

(2)测试顶端居中对齐方式,测试结果如下:

(3)测试顶端靠右对齐方式,测试结果如下:

转自:http://www.2cto.com/kf/201506/408343.html
标签:大小 平安 工作效率 字体 black extc bounds uila 重复
原文地址:http://www.cnblogs.com/freeleader/p/7389580.html