【问题标题】:How to change all UIButton fonts to custom font?如何将所有 UIButton 字体更改为自定义字体?
【发布时间】:2011-10-04 18:47:12
【问题描述】:

有没有办法做到这一点? 我想要一个我已经下载的自定义字体显示在 UIButton 上。

【问题讨论】:

标签: iphone xcode fonts interface-builder


【解决方案1】:

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 的文档非常好。尝试先在那里查找,然后搜索 SO,然后搜索 Google,然后写一个问题。 :)

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

#define BUTTON_FONT_NAME @"Helevetica"

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

【讨论】:

  • 是的,我知道,但是有没有办法一次更改所有按钮中的所有字体?
  • 我想你误会了我 :) 我想知道是否有避免这种情况: [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] 所以不用一一改了,有没有办法给所有设置默认字体用户界面按钮?
  • @Wikiboo:只需将其填充到子类中,然后将所有 UIButtons 的自定义类设置为该子类。编辑:就像帕特里克说的那样。
【解决方案2】:

您可以这样做的一种方法是子类化 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 设置。

【讨论】:

  • 这可行,但会强制您替换所有 UIButton 或添加其他方法。因此,无论如何,您都必须经历并做出改变。
  • 如果您将使用一个类别,您只需导入您的类别头文件
  • 不错。我想这是最不麻烦的方式。
【解决方案3】:

一个可能的选择是使用子类,然后编辑 init。

//MYButton.m
-(id) initWithTitle: (NSString *)title {
    self.titleLabel = title;
    self.titleLabel.font = [UIFont fontWithName: FONT_NAME size FONT_SIZE];
}

然后将所有 UIButtons 改为 MYButtons。

【讨论】:

    【解决方案4】:

    如果你使用IBOutletCollection,那么这应该是直接的。

    @property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
    

    将按钮连接到此插座集合,稍后使用,一次性更改字体,

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

    【讨论】:

    • 从技术上讲,它们是不同的字体。第一个论点必须改变。在Helvetica 的情况下,它将是Helvetica-Bold。它大多是可预测的,但不能保证。看看字体名称here
    • 如果不使用 IBOutletCollection 会怎样?
    【解决方案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】:

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

      【讨论】:

        猜你喜欢
        • 2012-12-19
        • 2014-04-05
        • 1970-01-01
        • 2023-04-02
        • 2012-11-29
        • 1970-01-01
        • 2013-08-23
        • 2011-05-13
        • 2011-11-05
        相关资源
        最近更新 更多