iOS Button按钮右上角文字

项目中经常遇到按钮右上角加文字的功能

重写button

#import <UIKit/UIKit.h>

@interface BadgeButton : UIButton

-(void)showBadgeWithNumber:(NSInteger)badgeNumber;

-(void)hideBadge;

@end


#import "BadgeButton.h"

#import "Masonry.h"

@interface BadgeButton()

@property (nonatomic,strong) UILabel* badgeLabel;

@end

@implementation BadgeButton

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        self.clipsToBounds = NO;

        [self.titleLabel setFont:[UIFont systemFontOfSize:15]];

        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        //实心角标

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

        

        空心角标

        self.badgeLabel = [[UILabel alloc]init];

        [self addSubview:self.badgeLabel];

        self.badgeLabel.backgroundColor = [UIColor clearColor];

        self.badgeLabel.font = [UIFont systemFontOfSize:10];

        self.badgeLabel.layer.borderWidth = 1;

        self.badgeLabel.layer.borderColor = [[UIColor greenColor] CGColor];

        self.badgeLabel.textColor = [UIColor redColor];

        self.badgeLabel.layer.cornerRadius = 6;

        self.badgeLabel.clipsToBounds = YES;

        //建立约束

        [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 - 显示角标

-(void)showBadgeWithNumber:(NSInteger)badgeNumber{

    self.badgeLabel.hidden = NO;

    self.badgeLabel.text = [NSString stringWithFormat:@" %ld ",badgeNumber];

}

#pragma mark - 隐藏角标

-(void)hideBadge{

    self.badgeLabel.hidden = YES;

}

实现方法

-(void)createUI{

    // 短按钮

    BadgeButton *badgeButton1 = [[BadgeButton alloc]initWithFrame:CGRectMake(90, 90, 50, 30)];

    [self.view addSubview:badgeButton1];

    [badgeButton1 setTitle:@"按钮1" forState:UIControlStateNormal];

    [badgeButton1 addTarget:self action:@selector(jumpToAnimationClick) forControlEvents:UIControlEventTouchUpInside];

    [badgeButton1 showBadgeWithNumber:92222];

    

    // 长按钮

    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);

    }];

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值