iOS开发小笔记 | 封装一个带角标的button

作者: 无夜之星辰 | 来源:发表于2017-03-22 11:02 被阅读573次
  • 先看设计图:


现在要封装的就是中间那个带角标的按钮。

  • 思路:

获取到button的titleLabel,再在titleLabel右上角放一个label。

  • 代码:

注:继承自UIButton

@interface BadgeButton ()

/** 显示按钮角标的label */
@property (nonatomic,strong) UILabel *badgeLabel;

@end

@implementation BadgeButton

#pragma mark - 构造方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // button属性设置
        self.clipsToBounds = NO;
        [self.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        //------- 角标label -------//
        self.badgeLabel = [[UILabel alloc]init];
        [self addSubview:self.badgeLabel];
        self.badgeLabel.backgroundColor = [UIColor redColor];
        self.badgeLabel.font = [UIFont systemFontOfSize:10];
        self.badgeLabel.textColor = [UIColor whiteColor];
        self.badgeLabel.layer.cornerRadius = 6;
        self.badgeLabel.clipsToBounds = YES;
        
        //------- 建立角标label的约束 -------//
        [self.badgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel.mas_right).mas_offset(-5);
            make.bottom.mas_equalTo(self.titleLabel.mas_top).mas_offset(5);
            make.height.mas_equalTo(12);
        }];
    }
    return self;
}

#pragma mark - 显示角标
/**
 显示角标

 @param badgeNumber 角标数量
 */
- (void)showBadgeWithNumber:(NSInteger)badgeNumber{
    self.badgeLabel.hidden = NO;
    // 注意数字前后各留一个空格,不然太紧凑
    self.badgeLabel.text = [NSString stringWithFormat:@" %ld ",badgeNumber];
}

#pragma mark - 隐藏角标
/** 隐藏角标 */
- (void)hideBadge{
    self.badgeLabel.hidden = YES;
}

@end
  • 封装的效果:

  • 上图的代码:

    // 短按钮
    BadgeButton *badgeButton1 = [[BadgeButton alloc]initWithFrame:CGRectMake(90, 90, 50, 30)];
    [self.view addSubview:badgeButton1];
    [badgeButton1 setTitle:@"按钮1" forState:UIControlStateNormal];
    [badgeButton1 showBadgeWithNumber:9];
    
    // 长按钮
    BadgeButton *badgeButton2 = [[BadgeButton alloc]initWithFrame:CGRectMake(90, 130, 90, 30)];
    [self.view addSubview:badgeButton2];
    [badgeButton2 setTitle:@"按钮22222" forState:UIControlStateNormal];
    [badgeButton2 showBadgeWithNumber:33];
    
    // Masonry布局的情况
    BadgeButton *badgeButton3 = [[BadgeButton alloc]init];
    [self.view addSubview:badgeButton3];
    [badgeButton3 setTitle:@"按钮33" forState:UIControlStateNormal];
    [badgeButton3 showBadgeWithNumber:99];
    [badgeButton3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(badgeButton2.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(90);
    }];

相关文章

网友评论

  • 一个很帅的蓝孩子:哈哈!学习了,最重要的还是角标前后加空格 😂
    无夜之星辰:@一个很帅的蓝孩子 是的,这波很关键
  • 谢谢生活:学习了,
    无夜之星辰:@谢谢生活 :smile:
  • 哈哈哈士奇XHB:用category就可以封装这个效果
    无夜之星辰:@哈哈哈士奇XHB 用类别给UIButton扩展一个构造方法也是可以的:smile:
  • moonCoder:每天看大神博客,月薪上万,不说了老板喊我搬砖了。
    无夜之星辰:@阿凡提de小毛驴 是的,不过masonry应该也算是个大众皆知的三方了吧:smile:
    3727939b0d64:你这是加了masonry吧?建议使用系统自带的autolayout或者手写布局,不然别人用你的button可能还需要import masonry:smile:
    无夜之星辰:@moonCoder 欧阳大神见笑了:sweat_smile:

本文标题:iOS开发小笔记 | 封装一个带角标的button

本文链接:https://www.haomeiwen.com/subject/hovgnttx.html