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