How to change all UIButton fonts to custom font?

15,220

Solution 1

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"];

Solution 2

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.

Solution 3

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/doc/uid/TP40006815

button.titleLabel.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];

Apple's documentation is quite good. Try looking it up there first, then search SO, then search Google, then write a question. :)

Edit: Well, the easiest way is to replace any fontWithName parameters with a constant, such as a macro.

#define BUTTON_FONT_NAME @"Helevetica"

If you haven't done that, then you will have to replace them all.

Share:
15,220
vburojevic
Author by

vburojevic

I'm a senior iOS developer with a degree in programming and algorithms and since 2012, I've been working hands-on with both Swift and Objective-C. I've developed dozens of apps using a range of tech and frameworks and also possess extensive experience in architectural design while working in teams and directly communicating with the clients. My utmost priority in all of my projects is to deliver high-quality products on time.

Updated on June 04, 2022

Comments

  • vburojevic
    vburojevic almost 2 years

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

  • vburojevic
    vburojevic almost 13 years
    Yes, I know that, but is there a way to change all fonts in all buttons at once?
  • vburojevic
    vburojevic almost 13 years
    I think you misunderstood me :) I want to know if there is avoid this: [code] button1.titleLabel.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE] button2.titleLabel.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE] button3.titleLabel.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE] [/code] So I don't have to change one by one, is there any way to set the default font for all UIButtons?
  • Trevor
    Trevor almost 13 years
    This would work, but will force you to replace all your UIButtons or add another method. So, regardless you will have to go through and make changes.
  • sudo rm -rf
    sudo rm -rf almost 13 years
    @Wikiboo: Just stuff it in a subclass and just set your custom class of all your UIButtons to that subclass. Edit: Just like Patrick said.
  • Cyprian
    Cyprian almost 13 years
    If you will use a category you will only have to import your category header file
  • Trevor
    Trevor almost 13 years
    Nice. I guess this is the least cumbersome way.
  • Deepak Danduprolu
    Deepak Danduprolu almost 13 years
    Technically, they are different fonts. The first argument will have to change. In Helvetica's case, it will be Helvetica-Bold. It is mostly predictable but isn't guaranteed. Take a look at the font names here.
  • brain56
    brain56 about 11 years
    What if you don't use IBOutletCollection?