自定义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很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...
随机推荐
- Silverlight安装成功后,提示安装
Silverlight安装成功后,提示安装. 解决方法1: 刷新浏览器,重新打开. 解决方法2: 打开>控制面板>添加删除程序>找到Sliverlight那个,卸载它就对了,那些 S ...
- 解迷宫的C++的未完善编程代码........请大神们帮忙改善下.........
这...................................................................... 我也是醉了 看不太懂,大神们求解............ ...
- Xcode8.0 去除控制台多余打印
选择Product->Scheme->Edit Scheme ...或者直接按 command + shift + < 快捷键,在弹出的窗口中Environment Variable ...
- 【分享】图解Windows Server 2012 R2 配置IIS 8全过程
最近计划更换服务器,包括IIS服务器与数据库服务器,操作系统都是Windows Server 2012 R2,第一次接触Windows Server 2012,感觉比较新鲜,一路摸索完成了IIS 8 ...
- android应用内存使用情况
单个应用程序最大内存限制,超过这个值会产生OOM(内存溢出) 命令:adb shell ->dalvik.vm.heapgrowthlimit 应用启动后分配的初始内存 命令:adb shell ...
- Jmeter - 源码开发环境配置
step1: 创建一个JavaProject , 我们命名为 JmeterSrcDev,点击Next.
- Java动态代理与Cglib库
JDK动态代理 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在 ...
- H5手机端关注的问题
手机端页面写css时应该注意的问题: 设备的屏幕尺寸 设备的分辨率 用户的操作习惯以及点击区域的大小 移动端浏览器的特性 鉴于分辨率等情况而引出的图片尺寸.布局方式等诸多细节问题 ...
- 微信微信JS-SDK 6.0.2 填坑笔记
0.为什么以前不需要配置这么麻烦就可以修改分享description 等信息,但是现在不行了. 因为6.0.2版本之前没有做权限验证,所以config都是ok,但这并不意味着你config中的签名是O ...
- 附加类型的实体失败,因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值
var list= DAL.LoadEntities(x => x.OrderCode == orderCode).AsNoTracking().ToList().FirstOrDefault( ...