如何将所有UIButton的字体更改为自定义字体?

4
有没有办法实现这个功能?我想让我下载的自定义字体在每个UIButton上显示。

LMGTFY :)https://dev59.com/i2855IYBdhLWcg3wnFtO - Patrick Perini
但是如何一次性更改所有按钮的字体? - vburojevic
6个回答

7
如果你使用IBOutletCollection,那么这应该是直接的。
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

将按钮连接到此出口集合,然后使用以下方法一次性更改字体:

[self setValue:[UIFont fontWithName:@"Helvetica" size:30] forKeyPath:@"buttons.font"];

从技术上讲,它们是不同的字体。第一个参数将需要更改。在 Helvetica 的情况下,它将是 Helvetica-Bold。它大多是可预测的,但不能保证。请查看字体名称这里 - Deepak Danduprolu
如果您不使用IBOutletCollection会怎样? - brain56

5
您可以通过创建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类别并在那里进行重写。
    //
    //  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 设置。

这个方法可以实现,但是会强制你替换所有的UIButtons或者添加另一个方法。所以,无论如何你都必须进行更改。 - Trevor
如果您使用类别,您只需要导入您的类别头文件。 - Cyprian
不错。我想这是最不繁琐的方式了。 - Trevor

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];

苹果的文档非常好。首先尝试在那里查找,然后搜索SO(Stack Overflow),再搜索Google,最后再写一个问题。 :)

编辑: 最简单的方法是将任何fontWithName参数替换为常量,例如宏。

#define BUTTON_FONT_NAME @"Helevetica"

如果您还没有这样做,那么您将不得不全部替换它们。

是的,我知道,但有没有一种方法可以同时更改所有按钮中的所有字体? - vburojevic
4
我想你误解了我的意思 :)我想知道是否有避免这种情况的方法: [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]这样就不需要逐个更改,是否有一种方式可以为所有UIButtons设置默认字体? - vburojevic
@Wikiboo:将它放入子类中,并将您所有UIButtons的自定义类设置为该子类。编辑:就像Patrick所说的那样。 - sudo rm -rf

0
一种可能的选择是使用子类,然后编辑init函数。
//MYButton.m
-(id) initWithTitle: (NSString *)title {
    self.titleLabel = title;
    self.titleLabel.font = [UIFont fontWithName: FONT_NAME size FONT_SIZE];
}

然后只需将所有的UIButtons更改为MYButtons即可。


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];

0
这对我有用: [self.btnTopics.titleLabel setFont:[UIFont fontWithName:@"System" size:12]];


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接