iOS Button按钮右上角文字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16588415/article/details/67639669

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

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

    }];

}




展开阅读全文

没有更多推荐了,返回首页