十一,iOS中的按鈕數字紅點提示

分類:編程 時間:2017-02-22

1,具體效果如圖


2,按鈕數字紅點其中的宏定義

#define SPColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

3,.h文件代碼

#import <UIKit/UIKit.h>

@interface BadgeButton : UIButton

@property (nonatomic) NSInteger badgeValue;

@property (nonatomic, assign) BOOL isRedBall;


@end

4,.m文件代碼

#import "BadgeButton.h"

@interface BadgeButton()

@property (nonatomic, strong) UILabel *badgeLab;

@end


@implementation BadgeButton

-(instancetype)init{
    self = [super init];
    if (self) {
        _badgeLab = [[UILabel alloc] init];
        _badgeLab.backgroundColor = SPColor(251, 85, 85, 1);
        _badgeLab.font = [UIFont systemFontOfSize:10];
        _badgeLab.textColor = [UIColor whiteColor];
        _badgeLab.clipsToBounds = YES;
        _badgeLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_badgeLab];
//        [self bringSubviewToFront:_badgeLab];
    }
    return self;
}
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    [self setSubFrame];
}

-(void)setIsRedBall:(BOOL)isRedBall{
    _isRedBall = isRedBall;
    [self setSubFrame];
}

-(void)setSubFrame{
    
    CGFloat badgeH;
    CGFloat badgeW;
    
    [self showText];
    
    if (_isRedBall) {
        badgeH = 8;
        badgeW = 8;
    }else{
        badgeH = 15;
        badgeW = [_badgeLab sizeThatFits:CGSizeMake(MAXFLOAT, badgeH)].width + 5;
        if (badgeW > 40) {
            badgeW = 40;
        }
        if (badgeW < badgeH) {
            badgeW = badgeH;
        }


    }
    
    
    _badgeLab.frame = CGRectMake(0, 0, badgeW, badgeH);
    _badgeLab.layer.cornerRadius = badgeH / 2;
    
    if (self.imageView.image) {
        CGPoint center = CGPointMake(CGRectGetMaxX(self.imageView.frame), self.imageView.frame.origin.y);
        _badgeLab.center = center;
    }else{
        CGPoint center = CGPointMake(self.bounds.size.width, self.bounds.origin.y);
        _badgeLab.center = center;
    }

}

-(void)setBadgeValue:(NSInteger)badgeValue{
    _badgeValue = http://www.ithao123.cn/badgeValue;
    [self setSubFrame];
}

-(void)showText{
    if (_badgeValue <= 0) {
        _badgeLab.hidden = YES;
    }else
        _badgeLab.hidden = NO;
        
    if (_isRedBall) {
        _badgeLab.text = @"";
    } else{
        if (_badgeValue > 99) {
            _badgeLab.text = @"99+";
        }else
            _badgeLab.text = [NSString stringWithFormat:@"%ld",(long)_badgeValue];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

5,具體的調用

_btn = [[BadgeButton alloc] init];
    [_btn setImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
    [_btn setTitleColor:DB_Blue forState:UIControlStateNormal];
    _btn.badgeValue = http://www.ithao123.cn/1;//紅點的值 _btn.isRedBall = YES;此bool值的設置只顯示紅點
    [self.view addSubview:_btn];




Tags: interface property frame return import

文章來源:


ads
ads

相關文章
ads

相關文章

ad