【发布时间】:2011-10-04 18:47:12
【问题描述】:
有没有办法做到这一点? 我想要一个我已经下载的自定义字体显示在 UIButton 上。
【问题讨论】:
-
LMGTFY :) stackoverflow.com/questions/4242361/…
-
但是如何一次更改所有按钮的所有字体?
标签: iphone xcode fonts interface-builder
有没有办法做到这一点? 我想要一个我已经下载的自定义字体显示在 UIButton 上。
【问题讨论】:
标签: iphone xcode fonts interface-builder
button.titleLabel.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];
Apple 的文档非常好。尝试先在那里查找,然后搜索 SO,然后搜索 Google,然后写一个问题。 :)
编辑:
嗯,最简单的方法就是将任何fontWithName 参数替换为常量,例如宏。
#define BUTTON_FONT_NAME @"Helevetica"
如果您还没有这样做,那么您将不得不全部替换它们。
【讨论】:
您可以这样做的一种方法是子类化 UIButton 设置您想要的字体并使用您的子类而不是 UIButton。
编辑
//
// 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]
您可以尝试的另一件事是为 UIButton 编写一个类别并在那里覆盖它。
编辑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
现在只需导入“UIButton+Font.h”,它将覆盖默认的 UIButton 设置。
【讨论】:
一个可能的选择是使用子类,然后编辑 init。
//MYButton.m
-(id) initWithTitle: (NSString *)title {
self.titleLabel = title;
self.titleLabel.font = [UIFont fontWithName: FONT_NAME size FONT_SIZE];
}
然后将所有 UIButtons 改为 MYButtons。
【讨论】:
如果你使用IBOutletCollection,那么这应该是直接的。
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
将按钮连接到此插座集合,稍后使用,一次性更改字体,
[self setValue:[UIFont fontWithName:@"Helvetica" size:30] forKeyPath:@"buttons.font"];
【讨论】:
Helvetica 的情况下,它将是Helvetica-Bold。它大多是可预测的,但不能保证。看看字体名称here。
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];
【讨论】:
这对我有用: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];
【讨论】: