Search code examples
iosxcode-storyboard

How to use programmatically defined font in storyboard in iOS?


I have so many UILabels and buttons in storyboard in a same style And I also have a list of fonts like

#define FLOAT_F1  [UIFont systemFontOfSize:18]
#define FLOAT_F2  [UIFont systemFontOfSize:16]

Can I use these defines in storyboard?So that I don't have to change it one by one in storyboard if the standards is changed.


Solution

  • You can create custom Label as shown below.

    File : UICustomLabel.h

    @interface UICustomLabel : UILabel
    
    @end
    

    File : UICustomLabel.m

    @implementation UICustomLabel
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    - (id)initWithCoder:(NSCoder *)coder {
        self = [super initWithCoder:coder];
        if (self) {
    
            // [UIFont fontWithName:@"AmericanTypewriter-Bold" size:self.font.pointSize]
            [self setFont:[UIFont fontWithName:@“Font_Name" size:self.font.pointSize]];
        }
        return self;
    }
    @end
    

    Then assign class UICustomLabel to each label in all storyboard you require to set as shown below.

    Set Custom Class To Label

    This will work for you. Please let me know if you have any problem.