Ios 为UIButton的不同状态设置不同字体

Ios 为UIButton的不同状态设置不同字体,ios,Ios,如何为不同的UIbutton状态设置不同的字体?谢谢你在这件事上的帮助 有关详细信息,您可以在设计视图上设置字体: 您可以在Interface Builder本身中设置所有这些。除非您有非常字符串的理由在代码中这样做。下面是如何在IB中执行此操作- 打开右侧栏,然后单击“状态配置”,在那里您可以看到按钮的各种状态,默认、突出显示、选中和禁用。现在,您可以为每个州设置不同的图像,为每个州设置不同的字体类型和字体颜色。希望这有帮助 谢谢 这是我的工作代码块IB_DESIGNABLE只是一个小小的改

如何为不同的UIbutton状态设置不同的字体?谢谢你在这件事上的帮助

有关详细信息,您可以在设计视图上设置字体:

您可以在Interface Builder本身中设置所有这些。除非您有非常字符串的理由在代码中这样做。下面是如何在IB中执行此操作-

打开右侧栏,然后单击“状态配置”,在那里您可以看到按钮的各种状态,默认、突出显示、选中和禁用。现在,您可以为每个州设置不同的图像,为每个州设置不同的字体类型和字体颜色。希望这有帮助


谢谢

这是我的工作代码块
IB_DESIGNABLE
只是一个小小的改进,可以使结果在界面生成器上可视化:-)

您可以在界面生成器中看到可设计的
MyButton
s字体,如下所示
只需创建自定义按钮即可。替代布局子视图。设置所需的字体

// Interface
@interface EezyButton : UIButton

@end
//Implementation
#import "EezyButton.h"

@implementation EezyButton

- (void)layoutSubviews{
    if (self.state == UIControlStateNormal) {
        [self.titleLabel setFont:[UIFont systemFontOfSize:12]];
    }
    else if (self.state == UIControlStateHighlighted){
        [self.titleLabel setFont:[UIFont systemFontOfSize:25]];

    }
    else if (self.state == UIControlStateDisabled){
        [self.titleLabel setFont:[UIFont systemFontOfSize:12]];
    }
    else if (self.state == UIControlStateSelected){
        [self.titleLabel setFont:[UIFont systemFontOfSize:28]];
    }
    [super layoutSubviews];
}

@end

这是一个非常酷的问题,它促使我创建了一个允许设置状态字体的
UIButton
子类

我还编写了一些示例代码,演示如何设置字体。如果使用的是Interface Builder,则将按钮的类别设置为
ConfigurableButton
。在代码中,按钮还必须声明为
ConfigurableButton
,因为我添加了新属性和
setFont:forState:
方法

请留下任何可以改进的评论

查看控制器的实现

#import "ViewController.h"
#import "ConfigurableButton.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet ConfigurableButton *toggleButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //Set the fonts for button's states
    _toggleButton.normalFont        = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:14];
    _toggleButton.highlightedFont   = [UIFont fontWithName:@"Chalkduster" size:14];
    _toggleButton.selectedFont      = [UIFont fontWithName:@"Zapfino" size:14];
    _toggleButton.disabledFont      = [UIFont fontWithName:@"Arial" size:14];
}

@end
ConfigurableButton.h

#import <UIKit/UIKit.h>

IB_DESIGNABLE

/**
 * A button that allows fonts to be assigned to each of the button's states.
 *
 * A state font can be specified using setFont:forState, or through one of the
 * four state Font properties.
 *
 * If a font is not specified for a given state, then
 * the System font will be displayed with a font size of 15.
 */
@interface ConfigurableButton : UIButton

@property (strong, nonatomic) UIFont *normalFont;
@property (strong, nonatomic) UIFont *highlightedFont;
@property (strong, nonatomic) UIFont *selectedFont;
@property (strong, nonatomic) UIFont *disabledFont;

/**
 * Set a font for a button state.
 *
 * @param font  the font
 * @param state a control state -- can be
 *      UIControlStateNormal
 *      UIControlStateHighlighted
 *      UIControlStateDisabled
 *      UIControlStateSelected
 */
- (void) setFont:(UIFont *)font forState:(NSUInteger)state;

@end

最简单的解决方案是为每个
UIControl
状态设置属性化标题:

var attributes = [String : AnyObject]()
attributes[NSForegroundColorAttributeName] = UIColor.redColor()
attributes[NSFontAttributeName] = UIFont.systemFontOfSize(15)

let normal = NSAttributedString(string: "normal", attributes: attributes)
button.setAttributedTitle(normal, forState: .Normal)

attributes[NSForegroundColorAttributeName] = UIColor.redColor()
attributes[NSFontAttributeName] = UIFont.boldSystemFontOfSize(15)

let selected = NSAttributedString(string: "selected", attributes: attributes)
button.setAttributedTitle(selected, forState: .Selected)

通常,我的代码如下:

itemButton.setAttributedTitle(NSAttributedString(string: "title", attributes: [NSAttributedString.Key.font : UIFont.font(ofSize: 16.0, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)]), for: .normal)
itemButton.setAttributedTitle(NSAttributedString(string: "title", attributes: [NSAttributedString.Key.font : UIFont.font(ofSize: 20.0, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.white]), for: .selected)

带有自定义字体的Swift 5版本:

todayButton.setAttributedTitle(NSAttributedString(string: "today", attributes: [.font: UIFont(name: "Font-Name", size: 16)!]), for: .normal)

todayButton.setAttributedTitle(NSAttributedString(string: "today", attributes: [.font: UIFont(name: "Font-Name", size: 24!]), for: .selected)

如果您使用Interface Builder,请不要忘记在它中设置按钮类型自定义

我想Dinesh想说的是,您可以使用不同的字体创建一个图像,然后将按钮设置为该图像。现在我更新答案,您可以按照uibutton中的“图像到状态”配置更改为“组合”,以更改uibutton不同状态的字体。很抱歉,前面没有清除。。我们怎么能不使用图像就通过编程实现呢。。。我这样问是因为,我从一个web服务中得到一个文本输入,它决定了按钮在不同状态下需要放置什么内容。你可以更改uibutton标题或标题字体吗?这并不能回答问题。他们想“为不同的UIbutton状态设置不同的字体”-而不是图像。@inforeqd请告诉我此解决方案是否适用于2019年的yoFeel Cool功能
itemButton.setAttributedTitle(NSAttributedString(string: "title", attributes: [NSAttributedString.Key.font : UIFont.font(ofSize: 16.0, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)]), for: .normal)
itemButton.setAttributedTitle(NSAttributedString(string: "title", attributes: [NSAttributedString.Key.font : UIFont.font(ofSize: 20.0, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.white]), for: .selected)
todayButton.setAttributedTitle(NSAttributedString(string: "today", attributes: [.font: UIFont(name: "Font-Name", size: 16)!]), for: .normal)

todayButton.setAttributedTitle(NSAttributedString(string: "today", attributes: [.font: UIFont(name: "Font-Name", size: 24!]), for: .selected)