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. explain的使用

    MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化. mysql. row ************ ...

  2. maven windows 环境变量

    MAVEN_HOME,内容是解压的maven文件路径 Path中添加 %MAVEN_HOME%\bin 为了测试安装成功,打开命令提示符,输入 mvn -version

  3. 2015 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...

  4. Angular 个人深究(一)【Angular中的Typescript 装饰器】

    Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...

  5. 利用transform的bug使fixed相对于父级定位

    首先,大家都清楚,元素使用fixed之后,若不设置top与left则会相对于最近的使用定位的父元素,并位于父元素的原点位置设置top与left值时,则会相对于窗口定位.但无论如何,此时仍相对于窗口定位 ...

  6. dsu on tree题表

    dsu on tree,又名树上启发式合并.重链剖分,是一类十分实用的trick,它常常可以作为一些正解的替代算法: 1.DFS序+线段树/主席树/线段树合并 2.对DFS序分块的树上莫队 3.长链剖 ...

  7. UOJ.52.[UR #4]元旦激光炮(交互 思路)

    题目链接 \(Description\) 交互库中有三个排好序的,长度分别为\(n_a,n_b,n_c\)的数组\(a,b,c\).你需要求出所有元素中第\(k\)小的数.你可以调用至多\(100\) ...

  8. 【转】Asp.net实现URL重写

    [概述] URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程.重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了 ...

  9. Windows 10官方原版ISO制作方法

    其实市面上的ISO原版都是这样的方法制作成光盘,然后再打包出来供人们下载的. 1.下载Windows 10安装程序工具: https://www.microsoft.com/zh-cn/softwar ...

  10. CentOS内核优化提示:cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: 没有那个文件或目录

    临时解决,重启失效 modprobe br_netfilter 为了开机加载上面这个模块 cat > /etc/rc.sysinit << EOF #!/bin/bash for f ...