这是一个简单的示例自定义按钮,它在 IB 中设置为按钮类:
#import "TestButton.h"
@implementation TestButton
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
UIFont * font = [UIFont systemFontOfSize:8];
if ( self.setFont )
self.titleLabel.font = font;
self.titleLabel.textColor = [UIColor redColor];
}
@end
如果 setFont 为 false,即字体不变,标签文字颜色如预期为红色。但如果它是真的,那么按钮文本颜色就是它在 IB 中设置的任何颜色。
所以问题是这里发生了什么,以及如何更改在 IB 中以编程方式分配的按钮的文本和字体。
如果有人想看看这些特性以及 IBDesignable 有多时髦,请参阅演示项目