-(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;
}

自定义BadgeView的更多相关文章

  1. ios资源

    ios 资源 分类: ios开发2012-05-30 16:39 573人阅读 评论(0) 收藏 举报 ios文档calendar2010reference图像处理 学习过程当中查找到的资料,做一个记 ...

  2. 【转】IOS开发资源汇总

    转自:http://blog.csdn.net/favormm/article/details/6664970 如何用Facebook graphic api上传视频: http://develope ...

  3. IOS地址

    IOS开发-你不可缺少的资源汇总-知识分享-转   如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ ...

  4. iPhone开发 - 常用库

    iPhone开发 - 常用库 这里总结了iPhone开发者开发过程中可能需要的一些资源 如何用Facebook graphic api上传视频: http://developers.facebook. ...

  5. (转)IOS 的一些资源汇总

      UI界面类项目: Panoramagl —— 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressH ...

  6. iPhone开发资源汇总

    如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...

  7. (转) iphone开发资源汇总

    如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...

  8. iOS tabbar 自定义小红点 消息显示,定制边框、颜色、高宽

    一般我们需要显示消息数,会利用到系统提供的api UIApplication.sharedApplication().applicationIconBadgeNumber = 10 但如果我们不想显示 ...

  9. BadgeView新提示开源工具类

    BadgeView是使用某个图标作为新功能的提醒,类似于收到短息后短信图标的右上方有信息数目或者其他的显示性提示.BadgeView很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...

随机推荐

  1. tinkphp登录验证码的使用

    登录和验证(控制器) <?php namespace Come\Controller; use Think\Controller; class RenController extends Con ...

  2. jquery.validate使用 - 常用验证脚本

    一些常用的验证脚本 不会写js了,只能从网上找一些常用的验证脚本. // 手机号码验证jQuery.validator.addMethod("mobile", function(v ...

  3. destoon系统中get_maincat的用法及解释

    get_maincat函数的用法, 如get_maincat(0, $mid, 1),其中第一.二.三个参数分别表示什么? 第一个参数代表parentid 第二个参数代表所有的分类 第三个参数 代表 ...

  4. hibernate的session对象核心方法注意的问题

    1.session.save()方法 1).session.save()方法会使一个对象从临时状态转变为持久化状态. 2).session.save()方法会赋予持久化对象的OID属性一个ID值,以对 ...

  5. Cocos2d-x lua 游戏中的菜单(Menu)

    菜单相关类包含 菜单(Menu)类 和 菜单项( MenuItem )类 Menu类图(派生于Layer) ref <-- Node <-Layer <-- Menu MenuIte ...

  6. iOS - Phone 电话

    1.调用电话 1.1 拨打系统电话 调用系统自带的打电话程序,要跳转到打电话程序,打完电话自动跳转回来. 在 iOS9.0 + 系统隐私控制里禁止查询设备中已安装的 App,所以在 iOS9.0 + ...

  7. 基础笔记12(socket,url网络通信)

    进一步深入socket 1.网络通信条件: .IP地址,可用主机名. .传输数据时将不用的应用程序通过数字标识区分开来,这种标识称为逻辑端口,也称端口.(0-65535端口,一般系统预留0-1024) ...

  8. 基础笔记10(IO 1.7try-with-resource) 装饰模式

    1.读写的类型分为字节流和字符流,字节流一般是视频音频其他所有的类型都可以. (非文档文件使用字符流易造成未知编码(?)错误) InputStream OutputStream 抽象类 fileInp ...

  9. 个人psp

    排球计分程序 1.计划 通过对用户故事估计这个任务需要3~5d天. 2.开发 2.1需求分析 作为一个观众,我希望了解每场比赛的比分,以便了解比赛的情况. 作为一个观众,我希望输入球队名称查询球队比分 ...

  10. spring aop 利用JoinPoint获取参数的值和方法名称

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...