Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change all UIButton fonts to custom font?

Is there any way to do this? I want to have a custom font that I've downloaded to show on ever UIButton.

like image 262
vburojevic Avatar asked Dec 07 '22 20:12

vburojevic


2 Answers

If you use IBOutletCollection then this should be direct.

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

Connect the buttons to this outlet collection and later alter the font in a single shot using,

[self setValue:[UIFont fontWithName:@"Helvetica" size:30] forKeyPath:@"buttons.font"];
like image 82
Deepak Danduprolu Avatar answered Dec 27 '22 12:12

Deepak Danduprolu


One way you could do it is to subclass UIButton setup your desired font and use your subclass instead of UIButton.

Edit

//
//  MyUIButton.h
//

@interface MyUIButton : UIButton {

}
+ (id)buttonWithType:(UIButtonType)buttonType;
@end

//
//  MyUIButton.m
//


#import "MyUIButton.h"


@implementation MyUIButton

+ (id)buttonWithType:(UIButtonType)buttonType{
   UIButton *button = [UIButton buttonWithType:buttonType];
   [[button titleLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
   return button;
}

@end

Then just use it like this:

[MyUIButton buttonWithType:UIButtonTypeCustom]

The other thing you can try is writing a category for UIButton and overriding it there.

Edit2

    //
    //  UIButton+Font.h
    //

#import <Foundation/Foundation.h>

@interface UIButton (DefaultFontExtension)
+ (id)buttonWithType:(UIButtonType)buttonType;
@end


    //
    //  UIButton+Font.m
    //

#import "UIButton+Font.h"

@implementation UIButton (DefaultFontExtension)

+ (id)buttonWithType:(UIButtonType)buttonType{
       UIButton *button = [UIButton buttonWithType:buttonType];
       [[button titleLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
       return button;
    }

@end

Now just import "UIButton+Font.h" it will override the default UIButton settings.

like image 31
Cyprian Avatar answered Dec 27 '22 12:12

Cyprian