平时需求,cell中或者头像都会有消息角标的出现,下面分享一个简单的角标控件,实现是很简单,可以根据自己的需求修改源码!
使用上直接给字体就可以适配大小,可以做到和系统角标差不多的效果。
调用:
self.badgeView = [[MKBadgeView alloc] initWithFrame:CGRectMake(self.view.center.x, self.view.center.y, 40, 40)];
[self.view addSubview:self.badgeView];
self.badgeView.count = 10;
self.badgeView.font = 18;
/** 数量 */
@property (nonatomic, assign) NSInteger count;
/** 字体大小 */
@property (nonatomic, assign) CGFloat font;
/** 文本颜色 */
@property (nonatomic, strong) UIColor *textColor;
/** 返回修改后的尺寸 */
- (CGSize)labelCount:(NSInteger)count;
/** 点击事件 */
- (void)didClickBlock:(void (^)(NSInteger))block;
#import "MKBadgeView.h"
@interface MKBadgeView ()
/** 默认的大小 18 字体 12 */
@property (nonatomic, assign) CGFloat labelWH;
@property (nonatomic, strong) UILabel *label;
@end
@implementation MKBadgeView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor redColor];
[self setDefaultParamter];
[self setupInit];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickBlock:)];
[self addGestureRecognizer:gesture];
}
return self;
}
- (void)setDefaultParamter {
_labelWH = self.frame.size.width ? : 18;
_font = _labelWH - 6;
_textColor = [UIColor whiteColor];
}
#pragma mark -
#pragma mark - action
- (void)didClickBlock:(void (^)(NSInteger))block {
block (_count);
}
#pragma mark -
#pragma mark - setter
- (void)setFont:(CGFloat)font {
_font = font ? : 12;
_labelWH = _font + 6;
[self setupInit];
self.count = _count;
}
- (void)setCount:(NSInteger)count {
_count = count;
[self labelCount:count];
}
- (CGSize)labelCount:(NSInteger)count {
_count = count;
self.hidden = !count;
if (count <= 0) {
return CGSizeZero;
}
self.label.frame = CGRectZero;
self.label.text = [NSString stringWithFormat:@"%zd",count];
if (count >= 100) {
self.label.text = [NSString stringWithFormat:@"%@",@"99+"];
}
if (count <= 0) {
self.label.text = [NSString stringWithFormat:@"%@",@"0"];
}
CGSize size = [self.label sizeThatFits:CGSizeZero];
CGFloat width = size.width < _labelWH ? _labelWH :size.width ;
// 加宽
width = count >= 10 ? width + _font/2 : width;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width, _labelWH);
self.label.frame = self.bounds;
return CGSizeMake(width, size.height);
}
#pragma mark -
#pragma mark - init
- (void)setupInit {
self.backgroundColor = [UIColor redColor];
self.layer.cornerRadius = _labelWH/2;
self.layer.masksToBounds = YES;
self.label.textColor = _textColor ;
self.label.font = [UIFont systemFontOfSize:_font];
}
- (UILabel *)label {
if (!_label) {
_label = [[UILabel alloc] initWithFrame:self.bounds];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
[self addSubview:_label];
}
return _label;
}
```objc