4

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

vburojevic
  • 1,666
  • 5
  • 24
  • 34

6 Answers6

7

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"];
Deepak Danduprolu
  • 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
  • What if you don't use IBOutletCollection? – brain56 Apr 11 '13 at 04:54
5

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.

Cyprian
  • 9,423
  • 4
  • 39
  • 73
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.

Trevor
  • 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
  • 4
    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? – 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
0
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];
Nithin M Keloth
  • 1,595
  • 1
  • 20
  • 37
0

This worked for me: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];

iPhoneDev
  • 2,995
  • 4
  • 33
  • 44
0

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.

Patrick Perini
  • 22,555
  • 12
  • 59
  • 88