//
// ZZBadgeButton.h
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/9/27.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZZBadgeButton :UIButton
@property (nonatomic,copy) NSString *value;
@end
//
// ZZBadgeButton.m
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/9/27.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import "ZZBadgeButton.h"
@implementation ZZBadgeButton
/**
* 首先设置尺寸
*/
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
self.hidden =YES;
self.userInteractionEnabled =NO;
self.titleLabel.font = [UIFontsystemFontOfSize:12];
[selfsetBackgroundImage:[UIImageresizedImageWithName:@"main_badge"]forState:UIControlStateNormal];
}
return self;
}
/**
* 高亮和普通都保持一致
*/
- (void)setHighlighted:(BOOL)highlighted{ }
/**
* 重写setter方法注意细节
*/
- (void)setValue:(NSString *)value
{
#pragma warning------- NSString 赋值要用copy
_value = [value copy];
// 1.设置可见性
if (value.length) {
self.hidden =NO;
// 2.设置尺寸
CGRect frame = self.frame;
frame.size.height =self.currentBackgroundImage.size.height;
if (value.length >1) {
CGSize valueSize = [value sizeWithFont:self.titleLabel.font];
frame.size.width = valueSize.width +10;
}else {
frame.size.width =self.currentBackgroundImage.size.width;
}
[supersetFrame:frame];
// 3.内容
[selfsetTitle:value forState:UIControlStateNormal];
}else {
self.hidden =YES;
}
}
- (void)setFrame:(CGRect)frame
{
frame.size =self.frame.size;
[supersetFrame:frame];
}
- (void)setBounds:(CGRect)bounds
{
bounds.size =self.bounds.size;
[supersetBounds:bounds];
}
@end