自定义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很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...
随机推荐
- JavaScript使用技巧(1)——JS常用的函数
1.字符串对象函数和属性 函数: charAt():返回在指定位置的字符. charCodeAt():返回在指定的位置的字符的 Unicode 编码. concat():连接字符串. indexOf( ...
- 怎样处理“error C2220: warning treated as error - no object file generated”错误
最近用VS2010 编译ceflib开源库是出现"怎样处理"error C2220: warning treated as error - no object file gener ...
- HQL查询——关联和连接
HQL查询--关联和连接 为了便于理解有关的使用关联和连接进行HQL查询,首先提供两个具有关联关系的持久化类:Person类和MyEvent类 Person类: import javax.persis ...
- vchar2和nvchar2
Oracle中varchar2 nvarchar2 VARCHAR2(size),可变长度的字符串,其最大长度为 size 个字节.size 的最大值是 4000,而最小值是 1.您必须指定一个 VA ...
- PCB走线分析——直角、差分、蛇形线
PCB直角走线的影响 布线(Layout)是PCB设计工程师最基本的工作技能之一.走线的好坏将直接影响到整个系统的性能,大多数高速的设计理论也要最终经过 Layout 得以实现并验证,由此可见,布 ...
- Dreamweaver架设网站终极教程
转自:http://www.cnblogs.com/loveme513/archive/2006/04/03/365969.html 一.定义站点 1. 在任意一个根目录下创建好一个文件夹(我们这里假 ...
- linux 最小安装 需要的后续操作
在centos7 最小安装后首先需要联网 设置dns vim /etc/resolv.conf dnsxiru 写入:nameserver 8.8.8.8nameserver 8.8.4.4 网络网关 ...
- 物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)
物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载) 2015年11月14日| by: nbboy| Category: 系统设计, 缓存设计, 高性能系统 摘要 ...
- javascript中利用柯里化函数实现bind方法
柯理化函数思想:一个js预先处理的思想:利用函数执行可以形成一个不销毁的作用域的原理,把需要预先处理的内容都储存在这个不销毁的作用域中,并且返回一个小函数,以后我们执行的都是小函数,在小函数中把之前预 ...
- 实验一 认识DOS
#include<stdio.h> #include<string.h> void main() { char cmd[20][20]={"dir&quo ...