在我的项目中,每个屏幕都有大量的 UILabels
可用,这就是为什么我为 UILabel
创建了一个单独的类,并且我想在其中设置所有标签属性那个类(class)。
但是标签属性和标签也没有添加到我的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]];
}
主视图 Controller :
#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 方法中
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
[self setupUI];
}
return self;
}
关于ios - 如何编写自定义 UILabel 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34586506/