自定义BadgeView
-(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的更多相关文章
- ios资源
ios 资源 分类: ios开发2012-05-30 16:39 573人阅读 评论(0) 收藏 举报 ios文档calendar2010reference图像处理 学习过程当中查找到的资料,做一个记 ...
- 【转】IOS开发资源汇总
转自:http://blog.csdn.net/favormm/article/details/6664970 如何用Facebook graphic api上传视频: http://develope ...
- IOS地址
IOS开发-你不可缺少的资源汇总-知识分享-转 如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ ...
- iPhone开发 - 常用库
iPhone开发 - 常用库 这里总结了iPhone开发者开发过程中可能需要的一些资源 如何用Facebook graphic api上传视频: http://developers.facebook. ...
- (转)IOS 的一些资源汇总
UI界面类项目: Panoramagl —— 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressH ...
- iPhone开发资源汇总
如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...
- (转) iphone开发资源汇总
如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...
- iOS tabbar 自定义小红点 消息显示,定制边框、颜色、高宽
一般我们需要显示消息数,会利用到系统提供的api UIApplication.sharedApplication().applicationIconBadgeNumber = 10 但如果我们不想显示 ...
- BadgeView新提示开源工具类
BadgeView是使用某个图标作为新功能的提醒,类似于收到短息后短信图标的右上方有信息数目或者其他的显示性提示.BadgeView很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...
随机推荐
- Java Programming Language Enhancements
引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...
- highcharts php请求mysql返回json数据作为数据源进行制图
直接上代码 [官方文档请参见http://www.highcharts.com/docs/working-with-data/getting-data-across-domains-jsonp] [实 ...
- term2 配置
item2是mac下非常好用的一款终端.但默认的配色实在不好用,经过一翻搜索终于找到了比较满意的. 1.先要修改~/.bash_profile.代码如下 2.选择喜欢的配色方案. 在Preferenc ...
- all ,any,abs的使用
#!/usr/bin/env python #all循环参数,如果每个元素都为真,那么all的返回值为真 r = all([True,'sad','asd']) print(r) #any 只有一个真 ...
- linux的三种安装软件包的方式(小白的学习之旅)
tar包,整个安装过程可以分为以下几步: 1) 取得应用软件:通过下载.购买光盘的方法获得: 2) 解压缩文件:一般tar包,都会再做一次压缩,如gzip.bz2等,所以你需要先解压.如果是最常见的g ...
- UVALive 3177 长城守卫
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- centos7 安装php7+mysql5.7+nginx+redis
.1.先修改yum源 https://webtatic.com rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest- ...
- mysql 存储过程和事件调度
存储过程(procedure): 建立一个存储过程需要知道的基础知识 1.确定输入/输出的参数和类型: IN tname varchar(20) 其中 IN 表示输入参数,tname 是参数名 va ...
- gradle相关配置内容解析
gradle 项目的构建工具,基于groovy语言.主要用于管理依赖包. as中一般将gradle下载在C:\Documents and Settings<用户名>.gradle\wrap ...
- 空间点绕轴旋转公式&程序(C++)
关键词:空间旋转.旋转轴 用途:相机位姿估计.无人机位姿估计.3D游戏.3D建模 文章类型:概念.公式总结(本文不带推倒过程,若想了解公式是如何推出来的请搜索文献),C++函数展示 @Author:V ...