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

inf*_*eqd 5 ios

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

She*_*mus 10

这是一个非常酷的问题,它促使我创建一个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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

ConfigurableButton.m

#import "ConfigurableButton.h"

@implementation ConfigurableButton

//Sets one of the font properties, depending on which state was passed
- (void) setFont:(UIFont *)font forState:(NSUInteger)state
{
    switch (state) {
        case UIControlStateNormal:
        {
            self.normalFont = font;
            break;
        }

        case UIControlStateHighlighted:
        {
            self.highlightedFont = font;
            break;
        }

        case UIControlStateDisabled:
        {
            self.disabledFont = font;
            break;
        }

        case UIControlStateSelected:
        {
            self.selectedFont = font;
            break;
        }

        default:
        {
            self.normalFont = font;
            break;
        }
    }
}

/**
 * Overrides layoutSubviews in UIView, to set the font for the button's state,
 * before calling [super layoutSubviews].
 */
- (void) layoutSubviews
{
    NSUInteger state = self.state;

    switch (state) {
        case UIControlStateNormal:
        {
            [self setTitleFont:_normalFont];
            break;
        }

        case UIControlStateHighlighted:
        {
            [self setTitleFont:_highlightedFont];
            break;
        }

        case UIControlStateDisabled:
        {
            [self setTitleFont:_disabledFont];
            break;
        }

        case UIControlStateSelected:
        {
            [self setTitleFont:_selectedFont];
            break;
        }

        default:
        {
            [self setTitleFont:_normalFont];
            break;
        }

    }

    [super layoutSubviews];
}

/**
 * Private
 *
 * Convenience method that falls back to the System font,
 * if no font is configured.
 */
- (void) setTitleFont:(UIFont *)font
{
    if (!font) {
        font = [UIFont systemFontOfSize:15];
    }

    self.titleLabel.font = font;
}

@end
Run Code Online (Sandbox Code Playgroud)


atx*_*txe 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)
Run Code Online (Sandbox Code Playgroud)


小智 5

通常,我的代码是这样的:

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)
Run Code Online (Sandbox Code Playgroud)


Din*_*esh -1

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

您可以在 Interface Builder 本身中设置所有这些。除非您有非常充分的理由在代码中执行此操作。以下是在 IB 中的操作方法 -

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

在此输入图像描述

谢谢..!