问题
Is there any way to do this? I want to have a custom font that I've downloaded to show on ever UIButton.
回答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"];
回答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.
回答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.
回答4:
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.
回答5:
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];
回答6:
This worked for me: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];
来源:https://stackoverflow.com/questions/6684240/how-to-change-all-uibutton-fonts-to-custom-font