Is there any way to do this? I want to have a custom font that I've downloaded to show on ever UIButton.
-
LMGTFY :) http://stackoverflow.com/questions/4242361/custom-font-title-for-uibutton – Patrick Perini Jul 13 '11 at 19:10
-
But how to change all fonts for all buttons at once? – vburojevic Jul 13 '11 at 19:16
6 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"];

- 44,595
- 12
- 101
- 105
-
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`](http://iosfonts.com/). – Deepak Danduprolu Jul 13 '11 at 20:06
-
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.

- 9,423
- 4
- 39
- 73
-
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. – Trevor Jul 13 '11 at 19:39
-
If you will use a category you will only have to import your category header file – Cyprian Jul 13 '11 at 19:44
-
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.

- 11,269
- 2
- 33
- 40
-
Yes, I know that, but is there a way to change all fonts in all buttons at once? – vburojevic Jul 13 '11 at 19:13
-
4I 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? – vburojevic Jul 13 '11 at 19:29
-
@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. – sudo rm -rf Jul 13 '11 at 19:40
UIButton *NameBtn=[UIButton buttonWithType:UIButtonTypeCustom];
NameBtn=[[UIButton alloc]init];
[NameBtn setBackgroundColor:[UIColor clearColor]];
[NameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
NSString *textb=[[self.searchList objectAtIndex:indexPath.row] objectForKey:@"name"];
CGSize constraintb = CGSizeMake(310 ,10);
CGSize sizeb = [textb sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraintb lineBreakMode:UILineBreakModeWordWrap];
[NameBtn setTitle:textb forState:UIControlStateNormal];
NameBtn.font = [UIFont fontWithName:@"Helvetica-Bold" size: 12.0];
NameBtn.frame = CGRectMake(85, 12, MAX(sizeb.width ,3.0f),12);
[NameBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NameBtn.tag=indexPath.row;
[cell addSubview:NameBtn];

- 1,595
- 1
- 20
- 37
This worked for me: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];

- 2,995
- 4
- 33
- 44
A possible option is to use a subclass, then edit the init.
//MYButton.m
-(id) initWithTitle: (NSString *)title {
self.titleLabel = title;
self.titleLabel.font = [UIFont fontWithName: FONT_NAME size FONT_SIZE];
}
Then just make all of your UIButtons MYButtons instead.

- 22,555
- 12
- 59
- 88