一:先看干嘛的
二:怎么用
//很多第三方显示1位时很爽 多位数字怎么都别扭 我支持自己调整间距
==================一下内容是旧版 有点问题 -_-!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
static CGFloat const margin = 5; //个位数时候 左右 上下间距
static CGFloat const marginTen = 8; //十位数字时候 左右 间距
DCBadgeModel *model = [[DCBadgeModel alloc]init];
model.badgeBgColor = [UIColor whiteColor]; //支持自定义背景颜色
model.badgefontSize = 9; //支持文字大小
model.badgefontColor = [UIColor blackColor]; //支持文字颜色
model.badgeEdgeWidth = 2; //支持边框的粗细
model.badgeEdgeColor = [UIColor redColor]; //支持边框的颜色
//badgeWidth 支持指定宽度
//badgeHeight 支持指定高度
//待扩展 自动99+ 今天不想写了
DCBadgeView * dcbageView = [DCBadgeView dcBadgeWithString:@"99" withModel:model];
dcbageView.frame = CGRectMake(100, 100, 100, 30);
dcbageView.backgroundColor = [UIColor clearColor];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dcbageView.dcBadgeString =@"99";
});
[self.view addSubview:dcbageView];
三:写这个方法很多 我是画的 上代码
DCBadgeView.h
//
// DCBadgeView.h
// DCBadge
//
// Created by point on 16/9/16.
// Copyright © 2016年 dacai. All rights reserved.
//
#import <UIKit/UIKit.h>
@class DCBadgeModel;
@interface DCBadgeView : UIView
+ (DCBadgeView *) dcBadgeWithString:(NSString *)badgeString withModel:(DCBadgeModel *)model;
@property(nonatomic,copy) NSString *dcBadgeString;
@property (nonatomic, assign) CGFloat progress;
@end
DCBadgeView.m
//
// DCBadgeView.m
// DCBadge
//
// Created by point on 16/9/16.
// Copyright © 2016年 dacai. All rights reserved.
//
#import "DCBadgeView.h"
#import "DCBadgeModel.h"
static CGFloat const margin = 5; //个位数时候 左右 上下间距
static CGFloat const marginTen = 8; //十位数字时候 左右 间距
@interface DCBadgeView ()
@property(nonatomic,strong) DCBadgeModel* dcBadgeModel;
@property(nonatomic,copy) NSString* badgeString;
@property(nonatomic,strong) UILabel* badgeLabel;
@property(nonatomic,assign) CGFloat stringMargin;
@end
@implementation DCBadgeView
+ (DCBadgeView *)dcBadgeWithString:(NSString *)badgeString withModel:(DCBadgeModel *)model{
return [[self alloc] initWithString:badgeString withModel:model];
}
- (id) initWithString:(NSString *)badgeString withModel:(DCBadgeModel *)model
{
_badgeString = badgeString;
self.dcBadgeModel = model;
CGSize size =[_badgeString sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:model.badgefontSize]}];
//指定大小 还是自动大小
if(self.dcBadgeModel.badgeHeight ==0){
self.dcBadgeModel.badgeHeight = size.height+margin;
}
if([badgeString integerValue]<=9){
_stringMargin = margin;
}else{
_stringMargin = marginTen;
}
if(self.dcBadgeModel.badgeWidth ==0){
if((size.width+_stringMargin)<self.dcBadgeModel.badgeHeight){
self.dcBadgeModel.badgeWidth = self.dcBadgeModel.badgeHeight;
}else{
self.dcBadgeModel.badgeWidth = size.width+_stringMargin;
}
}
self = [super initWithFrame:CGRectMake(0, 0, model.badgeWidth, model.badgeHeight)];
if(self!=nil) {
[self setupTitle];
}
return self;
}
- (void)setupTitle {
[self addSubview:self.badgeLabel];
self.badgeLabel.text = self.badgeString;
self.badgeLabel.frame = CGRectMake(0,0,self.dcBadgeModel.badgeWidth, self.dcBadgeModel.badgeHeight);
}
- (void)drawRect:(CGRect)rect {
if(self.dcBadgeModel.badgeEdgeWidth!=0){
UIBezierPath *roundedRectBorder = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.dcBadgeModel.badgeWidth, self.dcBadgeModel.badgeHeight) cornerRadius:self.dcBadgeModel.badgeHeight/2];
[self.dcBadgeModel.badgeEdgeColor setFill];
[roundedRectBorder fillWithBlendMode:kCGBlendModeNormal alpha:1];
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.dcBadgeModel.badgeEdgeWidth/2, self.dcBadgeModel.badgeEdgeWidth/2, self.dcBadgeModel.badgeWidth-self.dcBadgeModel.badgeEdgeWidth, self.dcBadgeModel.badgeHeight-self.dcBadgeModel.badgeEdgeWidth) cornerRadius:self.dcBadgeModel.badgeHeight/2];
[self.dcBadgeModel.badgeBgColor setFill];
[roundedRect fillWithBlendMode:kCGBlendModeNormal alpha:1];
}else{
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.dcBadgeModel.badgeWidth, self.dcBadgeModel.badgeHeight) cornerRadius:self.dcBadgeModel.badgeHeight/2];
[self.dcBadgeModel.badgeBgColor setFill];
[roundedRect fillWithBlendMode:kCGBlendModeNormal alpha:1];
}
}
- (void)setDcBadgeString:(NSString *)dcBadgeString {
_dcBadgeString = dcBadgeString;
if([_dcBadgeString integerValue]<=9){
_stringMargin = margin;
}else{
_stringMargin = marginTen;
}
CGSize size =[_dcBadgeString sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.dcBadgeModel.badgefontSize]}];
if((size.width+_stringMargin)<self.dcBadgeModel.badgeHeight){
self.dcBadgeModel.badgeWidth = self.dcBadgeModel.badgeHeight;
}else{
self.dcBadgeModel.badgeWidth = size.width+_stringMargin;
}
self.badgeLabel.text = _dcBadgeString;
self.badgeLabel.frame = CGRectMake(0,0,self.dcBadgeModel.badgeWidth, self.dcBadgeModel.badgeHeight);
[self setNeedsDisplay];
}
- (UILabel *)badgeLabel {
if(!_badgeLabel){
_badgeLabel = [[UILabel alloc]init];
_badgeLabel.font = [UIFont systemFontOfSize:_dcBadgeModel.badgefontSize];
_badgeLabel.textColor = _dcBadgeModel.badgefontColor;
_badgeLabel.textAlignment = NSTextAlignmentCenter;
[_badgeLabel sizeToFit];
}
return _badgeLabel;
}
@end
DCBadgeModel.h
//
// DCBadgeModel.h
// DCBadge
//
// Created by point on 16/9/16.
// Copyright © 2016年 dacai. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DCBadgeModel : NSObject
/** 宽 */
@property(nonatomic,assign) CGFloat badgeWidth;
/** 高 */
@property(nonatomic,assign) CGFloat badgeHeight;
/** 填充的颜色 */
@property(nonatomic,strong) UIColor * badgeBgColor;
/** 文字的大小 */
@property(nonatomic,assign) CGFloat badgefontSize;
/** 文字的颜色 */
@property(nonatomic,strong) UIColor *badgefontColor;
/** 边框的粗细 */
@property(nonatomic,assign) CGFloat badgeEdgeWidth;
/** 边框的颜色 */
@property(nonatomic,strong) UIColor *badgeEdgeColor;
@end