自定义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很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...
随机推荐
- 0.[WP Developer体验Andriod开发]之从零安装配置Android Studio并编写第一个Android App
0. 所需的安装文件 笔者做了几年WP,近来对Android有点兴趣,尝试一下Android开发,废话不多说,直接进入主题,先安装开发环境,笔者的系统环境为windows8.1&x64. 安装 ...
- 【前端】我的Gulp配置
2. gulp + browserify /** * File Name: gulpfile.js */ // 引入 gulp var gulp = require('gulp'); // 引入组件 ...
- Python 和 R 数据分析/挖掘工具互查
如果大家已经熟悉python和R的模块/包载入方式,那下面的表查找起来相对方便.python在下表中以模块.的方式引用,部分模块并非原生模块,请使用 pip install * 安装:同理,为了方便索 ...
- javascript回文和类名的检测方法
回文** //"123"变为"123321" //"abc321"变为"abcd321123cba" ...
- 美国 ZIP Code 一览表
今天给大家提供美国的Zip Code的原因是大家在注册国外的账号时,需要提供这个Zip Code,因为一般美国的服务默认是面向美国的,甚至是仅支持美国. 以下提供一些美国的zip code 列表. 邮 ...
- test homework ~ coverage about method printPrimes
/******************************************************* * Finds and prints n prime integers * Jeff ...
- php-sql-parser sql防注入脚本
<?php /** * SQL Parser from: http://code.google.com/p/php-sql-parser/ * License: New BSD */ class ...
- Express+mysql的博客(1)
学了东西一定要自己上手试过才知道是不是真的会了.一直想练练node的使用,本来也没有什么好想法的,经同学提醒了一下,发现其实我可以用node写一个博客.我同学说工作量会非常之大╮(╯_╰)╭那也得先试 ...
- python成长之路【第八篇】:异常处理
一.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! 语法: try: pass except Exc ...
- windows php线程安全和不安全,两个版本我也看不懂,记下来再说。
Windows下的PHP版本分两种:线程安全版本与非线程安全版本. 要论两者的区别,详细论说起来比较麻烦,从使用者的角度,记住什么时候用哪种版本的区别就可以了吧: 1.windows + IIS + ...