BadgeValueView

效果

源码

https://github.com/YouXianMing/UI-Component-Collection 中的 BadgeValueView

//
// BadgeValueView.h
// BadgeView
//
// Created by YouXianMing on 16/5/17.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> typedef NS_ENUM(NSUInteger, BadgePosition) { BadgePositionCenterLeft,
BadgePositionCenterRight, BadgePositionTopLeft,
BadgePositionTopRight, BadgePositionBottomLeft,
BadgePositionBottomRight,
}; @interface BadgeValueView : UIView /**
* bedge值
*/
@property (nonatomic, strong) NSString *badgeValue; /**
* 被附着的view
*/
@property (nonatomic, weak) UIView *contentView; /**
* 敏感字符增长宽度,默认值为4
*/
@property (nonatomic) CGFloat sensitiveTextWidth; /**
* 敏感增长宽度,默认为10
*/
@property (nonatomic) CGFloat sensitiveWidth; /**
* 固定高度,默认为20
*/
@property (nonatomic) CGFloat fixedHeight; /**
* 位置信息,默认为BadgePositionTopRight
*/
@property (nonatomic) BadgePosition position; /**
* 字体,默认为12
*/
@property (nonatomic, strong) UIFont *font; /**
* 字体颜色,默认为白色
*/
@property (nonatomic, strong) UIColor *textColor; /**
* bedge颜色,默认为红色
*/
@property (nonatomic, strong) UIColor *badgeColor; /**
* 开始生效
*/
- (void)makeEffect; /**
* 设置BadgeValue
*
* @param value BadgeValue
* @param animated 是否执行动画
*/
- (void)setBadgeValue:(NSString *)value animated:(BOOL)animated; @end
//
// BadgeValueView.m
// BadgeView
//
// Created by YouXianMing on 16/5/17.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "BadgeValueView.h"
#import "UIView+SetRect.h" @interface BadgeValueView () @property (nonatomic, strong) UILabel *label; @end @implementation BadgeValueView - (instancetype)init { if (self = [super init]) { self.sensitiveWidth = ;
self.fixedHeight = ;
self.sensitiveTextWidth = ;
self.position = BadgePositionTopRight;
self.font = [UIFont systemFontOfSize:.f];
self.textColor = [UIColor whiteColor];
self.badgeColor = [UIColor redColor];
} return self;
} - (void)makeEffect { // 标签
self.label = [[UILabel alloc] init];
self.label.textColor = self.textColor;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.font = self.font;
[self addSubview:self.label]; // 背景色
self.backgroundColor = self.badgeColor;
self.width = self.fixedHeight;
self.height = self.fixedHeight;
self.layer.cornerRadius = self.fixedHeight / .f;
self.layer.masksToBounds = YES; [_contentView addSubview:self];
} - (void)setBadgeValue:(NSString *)badgeValue animated:(BOOL)animated { _badgeValue = badgeValue; // 是否执行动画
if (animated) { [UIView animateWithDuration:0.15f animations:^{ self.alpha = badgeValue.length == ? : ;
}]; } else { self.alpha = badgeValue.length == ? : ;
} // 如果值为空,则不执行后续操作
if (badgeValue.length <= ) { return;
} // 设置文本
self.label.text = badgeValue;
[self.label sizeToFit]; // 更新尺寸
if (self.label.width + self.sensitiveTextWidth > self.width) { self.width += self.sensitiveWidth; } else { self.width = self.fixedHeight;
} // 更新文本尺寸
self.label.center = self.middlePoint; // 根据位置更新尺寸
CGFloat offset = self.fixedHeight / .f;
self.position == BadgePositionCenterLeft ? self.left = -offset, self.centerY = self.contentView.middleY : ;
self.position == BadgePositionCenterRight ? self.left = self.contentView.width - offset, self.centerY = self.contentView.middleY : ; self.position == BadgePositionTopLeft ? self.left = -offset, self.y = -offset : ;
self.position == BadgePositionTopRight ? self.top = -offset, self.left = self.contentView.width - offset : ; self.position == BadgePositionBottomLeft ? self.left = -offset, self.top = self.contentView.height - offset : ;
self.position == BadgePositionBottomRight ? self.left = self.contentView.width - offset, self.top = self.contentView.height - offset : ;
} - (void)setBadgeValue:(NSString *)badgeValue { [self setBadgeValue:badgeValue animated:NO];
} @end

BadgeValueView的更多相关文章

  1. 自定义按钮设置BadgeNumber

    TabbarButton.h @interface TabbarButton : UIButton @property (nonatomic, strong) UIButton *badgeValue ...

随机推荐

  1. 第二届CCF软件能力认证

    1. 相邻数对 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个 ...

  2. Spring Security + JWT实现前后端分离权限认证

    现在国内前后端很多公司都在使用前后端分离的开发方式,虽然也有很多人并不赞同前后端分离,比如以下这篇博客就很有意思: https://www.aliyun.com/jiaocheng/650661.ht ...

  3. 2019 B类

    警察 非警察

  4. gson 生成json有\u003d异常字符处理

    只需将Gson的初始化修改为 Gson gson = new GsonBuilder().disableHtmlEscaping().create(); 连接 http://blog.csdn.net ...

  5. HDU.5985.Lucky Coins(概率DP)

    题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...

  6. Redis 复制原理及分析

    1.测试 见master-slave测试帖 2 原理 第一次.Slave向Master同步的实现是: Slave向Master发出同步请求(发送sync命令),Master先dump出rdb文件,然后 ...

  7. 得到某个android应用 的POST和GET请求的 网址和参数

    两种思路: 1.用笔记本建个wifi热点,然后抓包. 2. 把APK装到模拟器上 会在任务管理中有个exe进程,用一个软件可以抓取这个进程的所有请求 .

  8. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  9. 修復 “Failed to bring up eth0″ in Ubuntu virtualbox

    参考文献: 修復 “Failed to bring up eth0″ in Ubuntu VMWare ubuntu下smokeping安装配置 背景 从同事那边拷贝了一个virtualbox虚拟机过 ...

  10. GO语言基础之method

    方法 method 1. Go 中虽没有 class,但依旧有 method 2. 通过显示说明 receiver 来实现与某个类型的组合 3. 只能为同一个包中的类型定义方法 4. Receiver ...