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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// GMUILabel.m
// Gengmei
//
// Created by Thierry on 12/26/14.
// Copyright (c) 2014 Wanmeichuangyi. All rights reserved.
//
#import <GMKit/GMLabel.h>
#import "GMFont.h"
#import <Constant.h>
@implementation GMLabel
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self setup];
}
- (void)setup{
_verticalAlignment = GMLabelVerticalAlignmentMiddle;
}
+ (GMLabel *)labelWithTextColor:(UIColor *)color fontSize:(CGFloat)fontSize{
GMLabel *lable = [GMLabel new];
lable.textColor = color;
lable.font = [UIFont gmFont:fontSize];
return lable;
}
+ (GMLabel *)labelWithTextAlignment:(NSTextAlignment)textAlignment backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor fontSize:(CGFloat )fontSize{
GMLabel *lable = [GMLabel new];
lable.textAlignment = textAlignment;
lable.textColor = textColor;
lable.backgroundColor = backgroundColor;
lable.font = [UIFont gmFont:fontSize];
return lable;
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor fontSize:(CGFloat )fontSize{
self.textAlignment = textAlignment;
self.textColor = textColor;
self.backgroundColor = backgroundColor;
self.font = [UIFont gmFont:fontSize];
}
- (void)setVerticalAlignment:(GMLabelVerticalAlignment)verticalAlignment {
_verticalAlignment = verticalAlignment;
[self setNeedsDisplay];
}
/**
* @author wangyang, 16-01-08 10:01:26
*
* @brief 这个方法在每次设置新的text值时会调用一次,并且只在最后调用一次。也就是说此时已经有了正确的size
* @note 不经重写的 UILabel
先调用 intrinsicContentSize
再调用 textRectForBounds:limitedToNumberOfLines
最后调用drawTextInRect
* @param requestedRect 等于self.bounds
* @since 5.9.0
*/
-(void)drawTextInRect:(CGRect)requestedRect {
// 先去除paddingEdge,剩下的rect就是用来放 string 的 rect。但注意:未必是正好能容纳下string的大小的rect
CGRect contentRect = UIEdgeInsetsInsetRect(requestedRect, self.paddingEdge);
// 在contentRect内,string所需要的text rect
CGRect textRect = [self textRectForBounds:contentRect limitedToNumberOfLines:self.numberOfLines];
// 将坐标信息清0,由我们自己控制
textRect.origin.x = 0;
textRect.origin.y = 0;
switch (self.verticalAlignment) {
case GMLabelVerticalAlignmentTop: {
textRect.origin.y = 0;
break;
}
case GMLabelVerticalAlignmentBottom: {
textRect.origin.y = requestedRect.size.height - textRect.size.height;
break;
}
case GMLabelVerticalAlignmentMiddle:
default: {
textRect.origin.y = (requestedRect.size.height - textRect.size.height)/2;
break;
}
}
switch (self.textAlignment) {
case NSTextAlignmentRight: {
textRect.origin.x = requestedRect.size.width - textRect.size.width;
break;
}
case NSTextAlignmentCenter: {
textRect.origin.x = (requestedRect.size.width - textRect.size.width)/2;
break;
}
case NSTextAlignmentNatural:
case NSTextAlignmentLeft:
default: {
textRect.origin.x = 0;
break;
}
}
[super drawTextInRect:textRect];
}
// 因为有paddingEdge,需要重写 intrinsicContentSize 方法。如果在约束里指定了size,该方法会失效
- (CGSize)intrinsicContentSize {
CGSize size = [super intrinsicContentSize];
CGSize newSize = CGSizeMake(ceilf(size.width + self.paddingEdge.left + self.paddingEdge.right), ceilf(size.height + self.paddingEdge.top + self.paddingEdge.bottom));
return newSize;
}
@end