Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing all UIButton font

Tags:

ios

uibutton

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?

like image 364
oopsi Avatar asked Mar 04 '14 11:03

oopsi


People also ask

How to change UIButton font?

To set a different font on UIButton programmatically you will need to create a new instance of UIFont object. The initializer of UIFont accepts two arguments: name and size. where the “GillSans-Italic” is a font name you want to set.

How do you change the font on UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How do I stop system font size changing effects to Android application?

You cannot stop the user from changing the system font size; you'll just have to use dp and ignore the lint errors!


3 Answers

In Swift3

UIButton.appearance().titleLabel?.font = font
like image 35
Rohit Kale Avatar answered Oct 24 '22 07:10

Rohit Kale


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

like image 150
Arturo Marzo Avatar answered Oct 24 '22 09:10

Arturo Marzo


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

like image 1
Ponf Avatar answered Oct 24 '22 07:10

Ponf