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

时间:2012-04-18 05:16:38

标签: ios

如何为不同的UI按钮状态设置不同的字体?感谢您对此的帮助。

7 个答案:

答案 0 :(得分:10)

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

我还写了一些示例代码来说明如何设置字体。如果您使用的是Interface Builder,则将按钮的类设置为UIButton。在代码中,该按钮也必须声明为ConfigurableButton,因为我添加了新属性和ConfigurableButton方法。

请对可以进行的任何改进发表评论!

查看控制器实施

setFont:forState:

<强> ConfigurableButton.h

#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.m

#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

答案 1 :(得分:5)

最简单的解决方案是为每个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)

答案 2 :(得分:2)

这是我的工作代码块。 IB_DESIGNABLE只是一个很小的改进,可以在Interface Builder上显示结果: - )

@interface MyButton : UIButton
@end

IB_DESIGNABLE @implementation MyButton
// Here you can override the look & feel for each state
// Actually not only fontSize, but any writable properties ^_^
- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    self.titleLabel.font = enabled ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:10];
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    self.titleLabel.font = highlighted ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:12];
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    self.titleLabel.font = selected ? [UIFont boldSystemFontOfSize:14] : [UIFont systemFontOfSize:12];
}

@end

您可以在界面构建器中看到可设计的MyButton字体,如下所示 enter image description here

答案 3 :(得分:2)

只需创建自定义按钮即可。覆盖布局子视图。设置所需的字体。

// 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

答案 4 :(得分:0)

通常,我这样编码:

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)

答案 5 :(得分:0)

带有自定义字体的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中设置“自定义”按钮类型

答案 6 :(得分:-1)

您可以在“设计视图”上设置字体以获取更多详细信息:

您可以在Interface Builder中设置所有这些。除非你有很多字符串理由在代码中执行此操作。以下是如何在IB中执行此操作 -

打开右侧栏杆&amp;然后点击“State Config”,你会看到按钮的各种状态,Default,Highlighted,Selected&amp;禁用。现在,您可以为每个州设置不同的图像,不同的字体类型和每个州的字体颜色。霍普这有助于......

enter image description here

谢谢..!