xcode 如何将所有 UIButton 字体更改为自定义字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6684240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change all UIButton fonts to custom font?
提问by vburojevic
Is there any way to do this? I want to have a custom font that I've downloaded to show on ever UIButton.
有没有办法做到这一点?我想要一个我下载的自定义字体以显示在 UIButton 上。
回答by Deepak Danduprolu
If you use IBOutletCollection
then this should be direct.
如果你使用, IBOutletCollection
那么这应该是直接的。
@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"];
回答by Cyprian
One way you could do it is to subclass UIButton setup your desired font and use your subclass instead of UIButton.
您可以做到的一种方法是将 UIButton 设置为您想要的字体并使用您的子类而不是 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.
您可以尝试的另一件事是为 UIButton 编写一个类别并在那里覆盖它。
Edit2
编辑2
//
// 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.
现在只需导入“UIButton+Font.h”,它将覆盖默认的 UIButton 设置。
回答by Trevor
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. :)
苹果的文档非常好。尝试先在那里查找,然后搜索 SO,然后搜索 Google,然后写一个问题。:)
Edit:Well, the easiest way is to replace any fontWithName
parameters with a constant, such as a macro.
编辑:嗯,最简单的方法是fontWithName
用常量替换任何参数,例如宏。
#define BUTTON_FONT_NAME @"Helevetica"
If you haven't done that, then you will have to replace them all.
如果您还没有这样做,那么您将不得不全部更换它们。
回答by Patrick Perini
A possible option is to use a subclass, then edit the init.
一个可能的选择是使用子类,然后编辑 init.d 文件。
//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.
然后只需将所有 UIButtons 改为 MYButtons。
回答by Nithin M Keloth
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];
回答by iPhoneDev
This worked for me: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];
这对我有用: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];