Configuring different fonts for different states of UIButton

How to set different fonts for different states of UIbutton? Thanks for your help in this.

+5
source share
7 answers

This is a very cool question that prompted me to make a subclass UIButtonthat allows you to set standard fonts!

I also wrote some sample code that shows how to install a font. If you are using Interface Builder, set the button class ConfigurableButton. In the code, the button should also be declared as ConfigurableButton, since I added new properties and a method setFont:forState:.

Please leave a comment for any improvements that can be made!

View controller implementation

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

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 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
+10

- 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)
+5

. IB_DESIGNABLE , 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

+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
+2

:

        button.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)
        button.setAttributedTitle(NSAttributedString(string: "title", attributes: [NSAttributedString.Key.font : UIFont.font(ofSize: 20.0, weight: .medium), NSAttributedString.Key.foregroundColor: UIColor.white]), for: .selected)
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)
0

Design View :

. . IB -

Open the right sidebar, and then click "Status Configuration", there you will see various button states: "Default", "Highlighted", "Highlighted" and "Disabled". Now you can set different images for each state, different font types and font colors for each state. Hopr it helps ...

enter image description here

Thank!..

-2
source

All Articles