【新浪微博项目】06--自定义BadgeButton
来源:互联网 发布:基于tensorflow的应用 编辑:程序博客网 时间:2024/06/05 00:20
1.BadgeButton的相关设置
IWBadgeButton.h
#import <UIKit/UIKit.h>@interface IWBadgeButton : UIButton@property (nonatomic, copy) NSString *badgeValue;@end
IWBadgeButton.m
#import "IWBadgeButton.h"#import "UIImage+MJ.h"@implementation IWBadgeButton- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.hidden = YES; self.userInteractionEnabled = NO;//设置背景图片 [self setBackgroundImage:[UIImage resizedImageWithName:@"main_badge"] forState:UIControlStateNormal];//设置按钮文字 self.titleLabel.font = [UIFont systemFontOfSize:11]; } return self;}- (void)setBadgeValue:(NSString *)badgeValue{#warning copy// _badgeValue = badgeValue; _badgeValue = [badgeValue copy]; if (badgeValue) { self.hidden = NO; // 设置文字 [self setTitle:badgeValue forState:UIControlStateNormal]; //[self setTitle:badgeValue forState:UIControlStateSelected]; // 设置frame CGRect frame = self.frame; CGFloat badgeH = self.currentBackgroundImage.size.height; CGFloat badgeW = self.currentBackgroundImage.size.width; if (badgeValue.length > 1) { // 文字的尺寸 CGSize badgeSize = [badgeValue sizeWithFont:self.titleLabel.font]; badgeW = badgeSize.width + 10; } frame.size.width = badgeW; frame.size.height = badgeH; self.frame = frame; } else { self.hidden = YES; }}@end
2.TabBarButton初始化的时候设置badgeButton
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 图标居中 self.imageView.contentMode = UIViewContentModeCenter; // 文字居中 self.titleLabel.textAlignment = NSTextAlignmentCenter; // 字体大小 self.titleLabel.font = [UIFont systemFontOfSize:11]; // 文字颜色 [self setTitleColor:IWTabBarButtonTitleColor forState:UIControlStateNormal]; [self setTitleColor:IWTabBarButtonTitleSelectedColor forState:UIControlStateSelected]; if (!iOS7) { // 非iOS7下,设置按钮选中时的背景 [self setBackgroundImage:[UIImage imageWithName:@"tabbar_slider"] forState:UIControlStateSelected]; } // 添加一个提醒数字按钮 IWBadgeButton *badgeButton = [[IWBadgeButton alloc] init]; badgeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:badgeButton]; self.badgeButton = badgeButton; } return self;}
3.TabBarButton中通过KVO监听按钮属性的改变
#import <UIKit/UIKit.h>@interface IWTabBarButton : UIButton@property (nonatomic, strong) UITabBarItem *item;@end
#import "IWTabBarButton.h"#import "IWBadgeButton.h"#import "Header.h"#import "UIImage+MJ.h"// 图标的比例#define IWTabBarButtonImageRatio 0.6#define IWColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]// 按钮的默认文字颜色#define IWTabBarButtonTitleColor (iOS7 ? [UIColor blackColor] : [UIColor whiteColor])// 按钮的选中文字颜色#define IWTabBarButtonTitleSelectedColor (iOS7 ? IWColor(234, 103, 7) : IWColor(248, 139, 0))@interface IWTabBarButton()/** * 提醒数字 */@property (nonatomic, weak) IWBadgeButton *badgeButton;@end@implementation IWTabBarButton- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 图标居中 self.imageView.contentMode = UIViewContentModeCenter; // 文字居中 self.titleLabel.textAlignment = NSTextAlignmentCenter; // 字体大小 self.titleLabel.font = [UIFont systemFontOfSize:11]; // 文字颜色 [self setTitleColor:IWTabBarButtonTitleColor forState:UIControlStateNormal]; [self setTitleColor:IWTabBarButtonTitleSelectedColor forState:UIControlStateSelected]; if (!iOS7) { // 非iOS7下,设置按钮选中时的背景 [self setBackgroundImage:[UIImage imageWithName:@"tabbar_slider"] forState:UIControlStateSelected]; } // 添加一个提醒数字按钮 IWBadgeButton *badgeButton = [[IWBadgeButton alloc] init]; badgeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin; [self addSubview:badgeButton]; self.badgeButton = badgeButton; } return self;}// 重写去掉高亮状态- (void)setHighlighted:(BOOL)highlighted {}// 内部图片的frame- (CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageW = contentRect.size.width; CGFloat imageH = contentRect.size.height * IWTabBarButtonImageRatio; return CGRectMake(0, 0, imageW, imageH);}// 内部文字的frame- (CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat titleY = contentRect.size.height * IWTabBarButtonImageRatio; CGFloat titleW = contentRect.size.width; CGFloat titleH = contentRect.size.height - titleY; return CGRectMake(0, titleY, titleW, titleH);}// 设置item- (void)setItem:(UITabBarItem *)item{ _item = item; // KVO 监听属性改变 [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil]; [item addObserver:self forKeyPath:@"title" options:0 context:nil]; [item addObserver:self forKeyPath:@"image" options:0 context:nil]; [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil]; [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];}- (void)dealloc{ [self.item removeObserver:self forKeyPath:@"badgeValue"]; [self.item removeObserver:self forKeyPath:@"title"]; [self.item removeObserver:self forKeyPath:@"image"]; [self.item removeObserver:self forKeyPath:@"selectedImage"];}/** * 监听到某个对象的属性改变了,就会调用 * * @param keyPath 属性名 * @param object 哪个对象的属性被改变 * @param change 属性发生的改变 */- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ // 设置文字 [self setTitle:self.item.title forState:UIControlStateSelected]; [self setTitle:self.item.title forState:UIControlStateNormal]; // 设置图片 [self setImage:self.item.image forState:UIControlStateNormal]; [self setImage:self.item.selectedImage forState:UIControlStateSelected]; // 设置提醒数字 self.badgeButton.badgeValue = self.item.badgeValue; // 设置提醒数字的位置 CGFloat badgeY = 5; CGFloat badgeX = self.frame.size.width - self.badgeButton.frame.size.width - 10; CGRect badgeF = self.badgeButton.frame; badgeF.origin.x = badgeX; badgeF.origin.y = badgeY; self.badgeButton.frame = badgeF;}@end
4.UIImage设置分类,拉伸图片
+ (UIImage *)resizedImageWithName:(NSString *)name{ UIImage *image = [self imageWithName:name]; return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];}
0 0
- 【新浪微博项目】06--自定义BadgeButton
- 自定义BadgeButton提醒数字
- 【新浪微博项目】05--自定义TabBarButton
- 【新浪微博项目】07--设置navigationItem和自定义TitleButton
- 自定义UITableViewCell新浪微博
- 新浪微博项目笔记
- 新浪微博如何自定义来源尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- PC端自定义新浪微博尾巴
- 陈正冲老师讲c语言之内存的申请malloc() 和释放free()
- windows,三个线程(+ ,- ,*);WaiForSingleObject;
- iOS设备分辨率、尺寸、图标大小等
- 时间管理总结
- Android动画开发——Animation动画效果详解
- 【新浪微博项目】06--自定义BadgeButton
- js中this的总结
- Spring Security 3 基于角色访问控制过程详解
- Zend Framework 1.x+jQuery EasyUI 1.3.4 整合二:构建项目
- __declspec(dllexport)、__declspec(dllimport)、__attribute__((visibility("default")))
- 位操作2
- UVa 10603 Fill
- 数据结构之顺序表的静态表示和动态表示
- GOF23设计模式之外观模式之实现