我有一个自定义按钮 AdvisorButton.h,在 init 中我设置了按钮的字体,但我相信它们被按钮的 Storyboard设置覆盖了。
实现如下:
-(id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
[self initializationCodeMethod];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
[self initializationCodeMethod];
}
return self;
}
-(id)initializationCodeMethod
{
if(self.tag == 1)
{
UIFont *theFont = [UIFont fontWithName:@"DINOT-Light" size:20];
[self.titleLabel setFont:theFont];
}
else
{
UIFont *theFont = [UIFont fontWithName:@"VWHeadlineOT-Book" size:20];
[self.titleLabel setFont:theFont];
}
return self;
}
字体对象已正确初始化,如果我将它们打印出来,则具有值等。
问题是它们是在 Storyboard中定义的,我想避免在它们的 View Controller 中引用每个按钮/标签/文本字段只是为了设置字体。
我认为 Storyboard 值是在 init 函数调用之后设置的吗?
对这个问题的任何洞察都会被证明是有用的!
编辑: 请注意,我在尝试设置之前检查了字体,并将其设置为 Storyboard 中的默认字体,这很好。
然后我在假定设置后再次检查它,它仍然完全相同。
最佳答案
确保您已在身份检查器部分为 Storyboard 中的相应按钮类添加自定义按钮类名称
也尝试在
中设置字体- (void)drawRect:(CGRect)rect
{
if ((self = [super initWithCoder:aDecoder]))
{
[self initializationCodeMethod];
}
return self;
}
在您的自定义按钮类中
关于ios - 设置自定义按钮字体而不被 Storyboard 中设置的值覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27269841/