自定义Label
- (instancetype)initWithFrame CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 1.初始化设置属性的值
_font = [UIFont systemFontOfSize:16];
_textColor = [UIColor blackColor];
_backgroundColor = [UIColor whiteColor];
// _textAlignment = NSTextAlignmentLeft;
}
return self;
}
- (void)setText NSString *)text
{
if (_text != text) {
_text = text;
}
[self setNeedsDisplay];
}
- (void)setTextColor UIColor *)textColor
{
if (_textColor != textColor) {
_textColor = textColor;
}
[self setNeedsDisplay];
}
- (void)setBackgroundColor UIColor *)backgroundColor
{
if (_backgroundColor != backgroundColor) {
_backgroundColor = backgroundColor;
}
[self setNeedsDisplay];
}
// 字体属性配置key注视Attributes--http://www.cnblogs.com/qingche/p/3574995.html
- (void)drawRect CGRect)rect {
// Drawing code
// 1.绘制文本
// 设置字体颜色
[_textColor setFill];
NSDictionary *dic = @{
NSFontAttributeName : _font,
NSForegroundColorAttributeName : _textColor,
NSBackgroundColorAttributeName : _backgroundColor
};
[_text drawInRect:rect withAttributes:dic];
}
效果图:
 |