123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // UIView+JYPBadgeView.m
- // QuanFangTong
- //
- // Created by apple on 2018/1/3.
- // Copyright © 2018年 xinkai. All rights reserved.
- //
- #define BAGEVIEW_HEIGHT 17
- #define BAGEVIEW_WIDTH1 17
- #define BAGEVIEW_WIDTH2 25
- #define BAGEVIEW_WIDTH3 35
- #import "UIView+JYPBadgeView.h"
- #import "UIView+XMGExtension.h"
- #import "UIView+ViewController.h"
- #import "View+MASAdditions.h"
- @implementation XKBadgeView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self createUI];
- }
- return self;
- }
- - (void)createUI {
-
- self.imageView = [[UIImageView alloc] init];
- [self addSubview:self.imageView];
- [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.bottom.right.equalTo(self);
- }];
-
- self.valueLab = [[UILabel alloc] init];
- self.valueLab.textAlignment = NSTextAlignmentCenter;
- self.valueLab.textColor = [UIColor whiteColor];
- self.valueLab.font = [UIFont systemFontOfSize:11.0];
- [self.imageView addSubview:self.valueLab];
- [self.valueLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.bottom.right.equalTo(self.imageView);
- }];
-
- }
- - (void)setBValue:(NSString *)bValue
- {
- _bValue = bValue;
- if(_bValue.length > 3){
- _bValue = @"99+";
- }
-
- CGFloat bageValueW = BAGEVIEW_HEIGHT;
- CGFloat bageValueH = BAGEVIEW_HEIGHT;
- self.valueLab.text = _bValue;
- // self.valueLab.text = @"";
- if (self.bValue.length == 1) {
- bageValueW = bageValueH;
- self.imageView.image = [UIImage imageNamed:@"bage_one"];
- }else if (self.bValue.length == 2){
- bageValueW = BAGEVIEW_WIDTH2;
- self.imageView.image = [UIImage imageNamed:@"bage_two"];
- }else{
- bageValueW = BAGEVIEW_WIDTH3;
- self.imageView.image = [UIImage imageNamed:@"bage_three"];
- }
-
- // self.imageView.image = [UIImage imageNamed:@"Home_红点"];
-
- self.width = bageValueW;
- self.height = bageValueH;
-
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(bageValueW);
- make.height.mas_equalTo(bageValueH);
- }];
-
- self.layer.cornerRadius = bageValueH/2;//圆形
- }
- @end
- @class XKBadgeView;
- @implementation UIView (JYPBadgeView)
- - (void)setBadgeValue:(NSString *)value title:(NSString *)title
- {
-
- XKBadgeView *badgeView = [[XKBadgeView alloc] initWithFrame:CGRectZero];
- [self addSubview:badgeView];
-
- // 确定小红点的位置
- [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(-(BAGEVIEW_HEIGHT/2));
- make.left.equalTo(self.mas_right).offset(0);
- make.width.height.mas_equalTo(BAGEVIEW_HEIGHT);
- }];
-
- if (![value isEqualToString:@"0"] && value != nil && value .length > 0 && [value integerValue] > 0) {
- badgeView.bValue = [NSString stringWithFormat:@"%@%@",title ,value];
- }else{
- badgeView.hidden = YES;
- [self removeAllSubviews];
- }
- }
- - (void)setBadgeValue:(NSString *)value
- {
- XKBadgeView *badgeView = [[XKBadgeView alloc] initWithFrame:CGRectZero];
- [self addSubview:badgeView];
- // 确定小红点的位置
- [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(-(BAGEVIEW_HEIGHT/2));
- make.left.equalTo(self.mas_right).offset(0);
- make.width.height.mas_equalTo(BAGEVIEW_HEIGHT);
- }];
-
- if (![value isEqualToString:@"0"] && value != nil && value .length > 0 && [value integerValue] > 0) {
- badgeView.bValue = value;
- }else{
- badgeView.hidden = YES;
- [self removeAllSubviews];
- }
- }
- - (void)setBadgeValue1:(NSString *)value
- {
- XKBadgeView *badgeView = [[XKBadgeView alloc] initWithFrame:CGRectZero];
- [self addSubview:badgeView];
- // 确定小红点的位置
- [badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(BAGEVIEW_HEIGHT/3);
- make.left.equalTo(self.mas_right).offset(-BAGEVIEW_HEIGHT/3);
- make.width.height.mas_equalTo(BAGEVIEW_HEIGHT);
- }];
-
- if (![value isEqualToString:@"0"] && value != nil && value .length > 0 && [value integerValue] > 0) {
- badgeView.bValue = value;
- }else{
- badgeView.hidden = YES;
- [self removeAllSubviews];
- }
- }
- @end
|