ios - 设置自定义按钮字体而不被 Storyboard 中设置的值覆盖

标签 ios objective-c xcode fonts xcode6.1

我有一个自定义按钮 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 中的相应按钮类添加自定义按钮类名称 Refer the image

也尝试在

中设置字体
- (void)drawRect:(CGRect)rect
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        [self initializationCodeMethod];
    }
    return self;
}

在您的自定义按钮类中

关于ios - 设置自定义按钮字体而不被 Storyboard 中设置的值覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27269841/

相关文章:

swift - 如何从 userNotificationCenter 中读取内容 - Swift

ios - Textfield 的约束与 UIButton 冲突

ios - Swift:无法使用 NSObjects 将类型 'NSNull' 的值转换为 'NSDictionary'

ios - 在所选区域中从 uiimagepickerviewcontroller 裁剪图像

objective-c - 应用程序试图以模态方式呈现事件 Controller : UIImagePickerController

ios - 如何使用Sprite Kit在不同设备上加载不同的 Sprite 表?

objective-c - 像 UITextField 一样隐藏 UITextview

ios - 标签文本是 'nil' ,尽管分配了一些文本

ios - 如何使用 UITableView 创建包含 UIWebView 的可 ScrollView

ios - 无法在初始加载时以编程方式设置 UINavigationItem 标题