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

最新文章

  1. 乱码引起的CSS失效原理,解决技巧。
  2. 大数据系列(4)——Hadoop集群VSFTP和SecureCRT安装配置
  3. Webpack--自学笔记
  4. iOS UIImage DownLoad图片的下载缓存全部在此
  5. angular bootstrap
  6. Java this 心得
  7. C++ 指针—02 指针与引用的对照
  8. es6中常用方法
  9. 性能测试过程中oracle数据库报ORA-27301 ORA-27302错
  10. JavaScript中Object值合并方法
  11. Docker Image管理学习笔记,ZT
  12. MATLAB:图像滤波,绝对值差(filter2,imabsdiff函数)
  13. 设计模式之Template(模板)(转)
  14. Nginx的location剖析
  15. 第六周课上测试-1-ch02
  16. 【hdu3555】 Bomb
  17. UVALive 5059 C - Playing With Stones 博弈论Sg函数
  18. solr进阶九:solr对数字和单个字符的搜索
  19. sumblime快捷键
  20. PYTHON 最佳实践指南(转)

热门文章

  1. FileInputStram入门
  2. 原 nc在centos7上的安装和简单使用
  3. 【PAT】1032 Sharing (25)(25 分)
  4. 【Java】 大话数据结构(3) 线性表之静态链表
  5. Python3.7.2,在Linux上跑来跑去的,是在升级打怪么?
  6. AngularJS初始化静态模板
  7. Java对epub电子书类型切割
  8. SQL2008配置管理工具服务显示远程过程调用失败
  9. C语言一些基础知识
  10. Prim算法和Kruskal算法