10

I've been looking for close to 2 hours and could not find a solid answer for this question.

I tried to use appearance mechanism but it's seems you can't change the font with it.

I've looked at the answer suggested here : How to change all UIButton fonts to custom font

There are 3 answer :

  1. use buttonOutlet, but this require an array which all button throughout the project most be linked to.
  2. using category, but from what I read it's highly discouraged to use categories in order to override existing methods.
  3. Using custom class that derive from UIButton, but when I try to use the supplied class and setting it in interface builder nothing happens, which cause me to believe you need to programitcally add your button.

Edit : with answer 3 if you implement the - (id)initWithCoder:(NSCoder *)aDecoder method of uibutton this enables you to simply change the class in interface builder, but you still need to do it for all the button in the project.

Does anyone have any new idea?

Community
  • 1
  • 1
oopsi
  • 1,919
  • 3
  • 21
  • 28
  • 1
    With 3, do you change the class of the button to your custom class in Interface Builder? – sbarow Mar 04 '14 at 11:45
  • I fixed my problem with 3. I needed to implement - (id)initWithCoder:(NSCoder *)aDecoder method. This is the one get called.. – oopsi Mar 04 '14 at 11:48
  • 1
    What does this have to do with the `xcode IDE`???? – Popeye Mar 04 '14 at 11:52
  • 2
    `[[UIButton appearance] setFont:[UIFont fontWithName:@"AmericanTypewriter" size:10.0]];`does work despite `setFont` being deprecated. – Mike Pollard Mar 04 '14 at 11:57

4 Answers4

6

In Swift3

UIButton.appearance().titleLabel?.font = font
RohitK
  • 1,444
  • 1
  • 14
  • 37
3

For some reason that only the gods from Apple know, the

[[[UIButton appearance] titleLabel] setFont:fontButton];

doesn't work. A solution that worked for me was found in swift in

How to set UIButton font via appearance proxy in iOS 8?

The solution in Objective-C is to create a category that exposes the titleLabel font like this

UIButton+TitleLabelFont.h

#import <UIKit/UIKit.h>

@interface UIButton (TitleLabelFont)

@property (strong, nonatomic) UIFont *titleFont;

@end

UIButton+TitleLabelFont.m

#import "UIButton+TitleLabelFont.h"

@implementation UIButton (TitleLabelFont)

- (UIFont *)titleFont {

    return self.titleLabel.font;
}

- (void)setTitleFont:(UIFont *)titleFont {

    self.titleLabel.font = titleFont;
}

@end

Then import the category UIButton+TitleLabelFont in the source that you're setting the appearance simply with

#import "UIButton+ASLAdditions.h"

and you'll be able to set the appearance with

[UIButton appearance].titleFont = fontButton;

Tested and worked perfectly for me

Community
  • 1
  • 1
1

You can use UIAppearance to customize view of any control. More information you can check out in this very helpful article on NSHipster.

Ponf
  • 1,190
  • 1
  • 12
  • 28
-1
for (UIButton *button in self.view.subviews) {
        if ([button isKindOfClass:UIButton.class]) {
            button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:14.0f];
        }
    }

Could work if you can't get the linked question to work.

Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92