我创建了一个UIButton的子类,给它添加了一个白色的2像素无边框,并且现在我正在尝试“全局”设置它的字体、字体颜色和背景颜色,具体取决于按钮的状态。
然而,字体没有被设置。颜色和背景颜色也都没有被设置。出了什么问题?以下是我的代码。希望你能帮忙 :)
然而,字体没有被设置。颜色和背景颜色也都没有被设置。出了什么问题?以下是我的代码。希望你能帮忙 :)
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[self titleLabel] setFont:[UIFont fontWithName:@"ZegoeUI" size:18]];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
if(self.state == UIControlStateHighlighted) {
[self setBackgroundColor:[UIColor whiteColor]];
}
else {
[self setBackgroundColor:[UIColor blackColor]];
}
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[self.layer setMasksToBounds:YES];
[self.layer setBorderWidth:2.0];
[self.layer setCornerRadius:0.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:1.0] CGColor]];
}
我不认为我做错了什么。 我有这个类,并在IB中链接了按钮,并设置了类类型。