自定义BadgeView

时间:2022-09-02 09:30:59

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.userInteractionEnabled=NO;
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        self.titleLabel.font=WBBadgeFont;
        [self sizeToFit];
    }
    return self;
}
/**重写set方法 当文字的宽度大于lab的宽度**/
-(void)setBadgeValue:(NSString *)badgeValue{
    _badgeValue=badgeValue;
    if (badgeValue.length==0 || [badgeValue isEqualToString:@"0"]) {
        self.hidden=YES;
    }else{
        self.hidden=NO;
    }
    NSMutableDictionary * dict=[NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName]=WBBadgeFont;
    CGSize size=[badgeValue sizeWithAttributes:dict];
    if (size.width>self.width) {
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        [self setTitle:nil forState:UIControlStateNormal];
        [self setBackgroundImage:nil forState:UIControlStateNormal];
    }else{
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        [self setTitle:badgeValue forState:UIControlStateNormal];
        [self setImage:nil forState:UIControlStateNormal];
    }
}

@property (nonatomic, strong) UITabBarItem *item;

#import "WBTabBarButton.h"

#import "WBBadge.h"
#import"UIView+Extension.m"
#define CZImageRidio 0.7
@interface WBTabBarButton ()

@property(nonatomic,weak)WBBadge * badgeView;
@end

@implementation WBTabBarButton
-(WBBadge *)badgeView{
    if (!_badgeView) {
        WBBadge * btn=[WBBadge buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
        _badgeView=btn;
    }
    return _badgeView;
}

-(void)setItem:(UITabBarItem *)item{
    _item=item;
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];
    [item addObserver:self forKeyPath:@"badageValue" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    [self setTitle:_item.title forState:UIControlStateNormal];
    
    [self setImage:_item.image forState:UIControlStateNormal];
    
    [self setImage:_item.selectedImage forState:UIControlStateSelected];
    self.badgeView.badgeValue=_item.badgeValue;
}
/**布局自控的位置**/
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.imageView
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageW = self.bounds.size.width;
    CGFloat imageH = self.bounds.size.height * CZImageRidio;
    self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
    
    
    // 2.title
    CGFloat titleX = 0;
    CGFloat titleY = imageH - 3;
    CGFloat titleW = self.bounds.size.width;
    CGFloat titleH = self.bounds.size.height - titleY;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    // 3.badgeView
    self.badgeView.x = self.width - self.badgeView.width - 10;
    self.badgeView.y = 0;
}