如何编写自定义 UILabel 类 [英] How to write a Custom UILabel Class
本文介绍了如何编写自定义 UILabel 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,每个屏幕都有大量的 UILabels
可用,这就是为什么我为 UILabel
创建了一个单独的类,我想在那个班.
In my project there is a huge number of UILabels
available for each screen that's why I have created one separate class for UILabel
and I want to set all label properties in that class.
但是标签属性和标签也没有添加到我的 MainViewController
中.
But label properties and label also not adding in my MainViewController
.
我的代码:
自定义标签:
#import "CustomLabel.h"
@implementation CustomLabel
- (id) init {
self = [super init];
if(self){
[self setupUI];
}
return self;
}
- (void)setupUI {
[self setBackgroundColor:[UIColor redColor]];
[self setTextColor:[UIColor blackColor]];
}
主视图控制器:
#import "MainViewController.h"
#import "CustomLabel.h"
@interface MainViewController ()
{
CustomLabel * mainLabel;
}
@end
@implementation SampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
mainLabel = [[CustomLabel alloc]initWithFrame:CGRectMake(50, 150, 100, 35)];
[self.view addSubview:mainLabel];
}
@end
推荐答案
awakeFromNib
不会被调用,如果你只是自己分配/初始化它,你需要把那个 [self setupUI];
在你应该覆盖的 init 方法中
awakeFromNib
doesnt get called if you just alloc/init it yourself afaik, you need to put that [self setupUI];
in the init method that you should override
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
[self setupUI];
}
return self;
}
这篇关于如何编写自定义 UILabel 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文