iOS使用带字体图标的UIButton(支持各种方向)

640?wx_fmt=jpeg

Linux编程 点击右侧关注,免费入门到精通! 640?wx_fmt=jpeg


1.图标字体的导入及使用: https://segmentfault.com/a/1190000004300281


2.我使用的是继承的方式,没有使用原生UIButton的title和imageView,而是自己增加两个UILabel到UIButton上去,好处是是按钮可以高度自定义,布局采用的是Masonry自适应。


3.相关代码:


ZMButton.h


 
 

#import <UIKit/UIKit.h>
/**
 *  默认图标在右边
 */

typedef NS_ENUM(NSInteger,ButtonIconType) {
    ButtonIconTypeNormal = 0,
    ButtonIconTypeLeft,
    ButtonIconTypeTop,
    ButtonIconTypeBottom
};
@interface ZMButton : UIButton

/** 标题标签 */
@property (nonatomicstrongUILabel       *buttonTitleLabel;
/** 字体图标标签 */
@property (nonatomicstrongUILabel       *buttonIconLabel;
/** 标题 */
@property (nonatomiccopy)   NSString      *buttonTitle;
/** 图标 */
@property (nonatomiccopy)   NSString      *buttonIcon;
/** 图标类型 */
@property (nonatomicassign) ButtonIconType iconType;
/** 公共间距 */
@property (nonatomicassignCGFloat        margin;
/** 左间距 */
@property (nonatomicassignCGFloat        marginLeft;
/** 上间距 */
@property (nonatomicassignCGFloat        marginTop;
/** 按钮总宽度(包含间距) */
@property (nonatomicassignCGFloat        totalWidth;
/** 字体 */
@property (nonatomicstrongUIFont         *titleFont;
/** 字体大小 */
@property (nonatomicassignCGFloat        titleFontSize;
/** 字体size */
@property (nonatomicassignCGSize         titleSize;
/** 图标字体 */
@property (nonatomicstrongUIFont         *iconFont;
/** 图标字体大小 */
@property (nonatomicassignCGFloat        iconFontSize;
/** 图标size */
@property (nonatomicassignCGSize         iconSize;
/** 标题颜色 */
@property (nonatomicstrongUIColor        *titleColor;
/** 图标颜色 */
@property (nonatomicstrongUIColor        *iconColor;

- (instancetype)initWithTitle:(NSString *)title  icon:(NSString *)icon iconType:(ButtonIconType)iconType;
- (void)setupUI;

@end


ZMButton.m


 
 

#import "ZMButton.h"

@implementation ZMButton

- (UILabel *)buttonTitleLabel{
    if (!_buttonTitleLabel) {
        _buttonTitleLabel = [[UILabel alloc] init];
        _buttonTitleLabel.font = _titleFont;
        _buttonTitleLabel.textColor = _titleColor;
        [self addSubview:_buttonTitleLabel];
        [_buttonTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (_iconType == ButtonIconTypeNormal) {
                make.left.mas_equalTo(self.marginLeft);
                make.centerY.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeLeft){
                make.centerY.mas_equalTo(self);
                make.left.mas_equalTo(self.buttonIconLabel.mas_right).with.offset(_margin);
                //make.right.mas_equalTo(-_margin);
            }else if (_iconType == ButtonIconTypeTop){
                make.top.mas_equalTo(self.buttonIconLabel.mas_bottom).with.offset(_margin);
                make.height.mas_equalTo(_titleSize.height);
                make.centerX.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeBottom){
                make.top.mas_equalTo((self.height - _titleSize.height - _iconSize.height - _margin)/2);
                make.height.mas_equalTo(_titleSize.height);
                make.centerX.mas_equalTo(self);
            }
        }];
    }
    return _buttonTitleLabel;
}

- (UILabel *)buttonIconLabel{
    if (!_buttonIconLabel) {
        _buttonIconLabel = [[UILabel alloc] init];
        _buttonIconLabel.font = [ZMFont iconOutlineFontWithSize:_iconFontSize];
        _buttonIconLabel.textColor = _iconColor;
        [self addSubview:_buttonIconLabel];
        [_buttonIconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (_iconType == ButtonIconTypeNormal) {
                //make.right.mas_equalTo(-_margin);
                make.left.mas_equalTo(self.buttonTitleLabel.mas_right).with.offset(_margin);
                make.centerY.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeLeft){
                make.left.mas_equalTo(self.marginLeft);
                make.centerY.mas_equalTo(self);
            }else if (_iconType == ButtonIconTypeTop){
                make.top.mas_equalTo(self.marginTop);
                make.centerX.mas_equalTo(self);
                make.height.mas_equalTo(_iconSize.height);
            }else if (_iconType == ButtonIconTypeBottom){
                make.top.mas_equalTo(self.buttonTitleLabel.mas_bottom).with.offset(_margin);
                make.height.mas_equalTo(_iconSize.height);
                make.centerX.mas_equalTo(self);
            }
        }];
    }
    return _buttonIconLabel;
}

#pragma mark - 间距的get方法
- (CGFloat)marginLeft{
    return (self.width - _iconSize.width - _titleSize.width - _margin) /2;
}

- (CGFloat)marginTop{
    return (self.height - _titleSize.height - _iconSize.height - _margin)/2;
}

- (instancetype)initWithTitle:(NSString *)title  icon:(NSString *)icon iconType:(ButtonIconType)iconType{
    self = [super init];
    if (self) {
        self.layer.borderWidth = 1/YYScreenScale();
        self.layer.borderColor = [UIColor blackColor].CGColor;
        NSAssert(title.length, @"title is null");
        NSAssert(icon.length, @"icon is null");
        _buttonTitle = title;
        _buttonIcon  = icon;
        _iconType = iconType;
        _titleFontSize = 15;
        _iconFontSize = 15;
        _margin = 5;
        _titleFont = [UIFont systemFontOfSize:_titleFontSize];
        _iconFont = [ZMFont iconOutlineFontWithSize:_iconFontSize];
        _titleColor = [UIColor colorWithHexString:@"#757374"];
        _iconColor = [UIColor colorWithHexString:@"#757374"];

        _titleSize = [title sizeForFont:_titleFont size:CGSizeMake(kScreenWidth, _titleFontSize * 2) mode:NSLineBreakByWordWrapping];
        _iconSize = [icon sizeForFont:_iconFont size:CGSizeMake(kScreenWidth, _iconFontSize * 2) mode:0];

        [self getTotalWidth];
    }
    return self;
}

#pragma mark - 设置按钮数据
- (void)setupUI{
    self.buttonTitleLabel.text = self.buttonTitle;
    self.buttonIconLabel.text = self.buttonIcon;
}

#pragma mark - 设置标题数据 (调用此方法可重写设置按钮标题)
- (void)setButtonTitle:(NSString *)buttonTitle{
    if (buttonTitle.length) {
        _buttonTitle = buttonTitle;
        self.buttonTitleLabel.text = buttonTitle;
        _titleSize = [_buttonTitle sizeForFont:_titleFont size:CGSizeMake(kScreenWidth, _titleFontSize * 2) mode:NSLineBreakByWordWrapping];
        [self getTotalWidth];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.totalWidth);
        }];
        [self.superview layoutIfNeeded];
        self.marginLeft = (self.width - _titleSize.width - _iconSize.width - _margin)/2;
        if (self.iconType == ButtonIconTypeNormal) {
            [self.buttonTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.marginLeft);
            }];
        }else if (self.iconType == ButtonIconTypeLeft) {
            [self.buttonIconLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.marginLeft);
            }];
        }
    }
}

#pragma mark - 计算总宽度
- (void)getTotalWidth{
    if (_iconType == ButtonIconTypeNormal || _iconType == ButtonIconTypeLeft) {
        self.totalWidth = _titleSize.width + _iconSize.width + _margin * 3;
    }else{
        self.totalWidth = _titleSize.width + _margin * 2;
    }
}

@end


4.如何使用:


 
 

//图标在左
    ZMButton *iconButtonLeft = [[ZMButton alloc] initWithTitle:@"文字在右" icon:@"U0000e6dfU0000ea9b" iconType:ButtonIconTypeLeft];
    iconButtonLeft.margin = 10;
    iconButtonLeft.titleColor = [UIColor colorWithHexString:@"#DC143C"];
    iconButtonLeft.tag = 1;
    [self.view addSubview:iconButtonLeft];
    [iconButtonLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(120);
        make.centerX.mas_equalTo(self.view);
    }];
    [iconButtonLeft.superview layoutIfNeeded];
    [iconButtonLeft setupUI];
    [iconButtonLeft addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    //图标在右
    ZMButton *iconButtonRight = [[ZMButton alloc] initWithTitle:@"文字在左" icon:@"U0000e6df" iconType:ButtonIconTypeNormal];
    [self.view addSubview:iconButtonRight];
    [iconButtonRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iconButtonLeft.mas_bottom).with.offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(40);
    }];
    [iconButtonRight.superview layoutIfNeeded];
    [iconButtonRight setupUI];
    [iconButtonRight addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];


5.相关界面,初始化按钮之后,文本也可以随时修改,支持多个图标,代码中加入了FLEX,可以摇一摇试试~


640?wx_fmt=png

 推荐↓↓↓ 

640?wx_fmt=png

?16个技术公众号】都在这里!

涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。

640?wx_fmt=png万水千山总是情,点个 “ 好看” 行不行
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值