美文网首页
底部消息数目

底部消息数目

作者: i爱吃土豆的猫 | 来源:发表于2020-02-29 23:18 被阅读0次

.h

#import <UIKit/UIKit.h>

@interface UITabBar (badge)

- (void)showBadgeOnItemIndex:(int)index num:(NSInteger )num;
- (void)hideBadgeOnItemIndex:(int)index num:(NSInteger )num;

@end

.m

#import "UITabBar+badge.h"

#define TabbarItemNums 4.0

@implementation UITabBar (badge)
//显示红点
- (void)showBadgeOnItemIndex:(int)index num:(NSInteger )messageCount{
    [self removeBadgeOnItemIndex:index];
    //新建小红点
    //    UIView *bview = [[UIView alloc]init];
    //    bview.tag = 888+index;
    //    bview.layer.cornerRadius = 5;
    //    bview.clipsToBounds = YES;
    //    bview.backgroundColor = [UIColor redColor];
    //    CGRect tabFram = self.frame;

    UILabel *lab = [[UILabel alloc]init];
    lab.tag = 888+index;
    if (messageCount>99) {
        lab.text = [NSString stringWithFormat:@"%@",@"99+"];
    }else{
        lab.text = [NSString stringWithFormat:@"%ld",(long)messageCount];
    }

    lab.textAlignment = NSTextAlignmentCenter;
    lab.clipsToBounds = YES;
    lab.font = [UIFont systemFontOfSize:12];
    lab.textColor = [UIColor whiteColor];
    lab.backgroundColor = [UIColor redColor];
    CGRect tabFram = self.frame;

    float percentX = (index+0.6)/TabbarItemNums;
    CGFloat x = ceilf(percentX*tabFram.size.width);
    CGFloat y = ceilf(0.05*tabFram.size.height);
    CGFloat width = [self getStrWidthWithStr:lab.text  andFont:14];
//    lab.frame = CGRectMake(x, y, width, 14);
    CGFloat rwidth;
    CGFloat rheight;
        if (messageCount<10) {
            rwidth = 15;
            rheight = 14;
        }else if (10<= messageCount && messageCount <99) {
            rwidth = 25;
            rheight = 14;
        }else{
            rwidth = 35;
            rheight = 14;
        }

    lab.layer.cornerRadius = rheight/2;
    lab.frame = CGRectMake(x, y, rwidth, rheight);
    [self addSubview:lab];
    [self bringSubviewToFront:lab];
}

//隐藏红点
- (void)hideBadgeOnItemIndex:(int)index num:(NSInteger )num{
    [self removeBadgeOnItemIndex:index];
}
//移除控件
- (void)removeBadgeOnItemIndex:(int)index{
    for (UIView*subView in self.subviews) {
        if (subView.tag == 888+index) {
            [subView removeFromSuperview];
        }
    }
}

/**
 计算一行文字宽度
 */
- (CGFloat)getStrWidthWithStr:(NSString *)str andFont:(CGFloat)fontFloat
{
    UIFont *font = [UIFont systemFontOfSize:fontFloat];
    NSDictionary *dic = @{NSFontAttributeName:font};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return rect.size.width;
}

@end

调用的地方

//未读消息数量
NSString *msgNum = initIndexDataModel.msgNum;
if ([msgNum integerValue] == 0) {
      [self.tabBarController.tabBar hideBadgeOnItemIndex:3 num:[msgNum integerValue]];
 }else{
       [self.tabBarController.tabBar showBadgeOnItemIndex:3 num:[msgNum integerValue]];
 }

相关文章

网友评论

      本文标题:底部消息数目

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