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. java 异常的限制

    一. 1.) 在覆盖方法的时候,只能抛出在基类方法的异常说明里列出的那些异常 2.) 在基类构造器声明的异常,在子类必须抛出,子类的构造器可以抛出任何异常,但是必须抛出基类构造器的异常 3.) 在基类 ...

  2. Python PIL: cannot write mode RGBA as BMP(把有四位通道(RGBA)的图片换成有三位通道的(RGA))

    图片png结尾的有四个通道RGBA,把四个通道改为三个通道(RGB),如下,bg为修改后的图片 img = Image.open('pic\\find.png')bg = Image.new(&quo ...

  3. RSS新手必读

    当谷歌停止Google Reader后,我开始玩RSS Reader了.网上大抵说Google Reader的退出很可惜,不过替代品还是存在的.   作为一个newbie我的视野或许很局限不过还是说几 ...

  4. BootStrap fileinput.js文件上传组件实例代码

    1.首先我们下载好fileinput插件引入插件 ? 1 2 3 <span style="font-size:14px;"><link type="t ...

  5. .NetCore下使用Prometheus实现系统监控和警报 (一)介绍【译】

    [译]原文https://prometheus.io/docs/introduction/overview 什么是Prometheus? Prometheus是一个开源系统监控和警报工具包,最初起源于 ...

  6. 自定义yum源

    1.创建rpm包的存放目录 mkdir  -p   /yum/yum-sum/package 2.准备rpm包,可以通过自带yum只下载不安装工具下载 yum install --downloadon ...

  7. 【LOJ】#2534. 「CQOI2018」异或序列

    题解 每个数都处理成前缀和,就相当于问\([l - 1,r]\)有几个数对\(x,y\),\(sum[x] ^ sum[y] = k\) 直接莫队即可 代码 #include <bits/std ...

  8. 有关redis相关的性能优化及内存说明

    本篇文章不涉及redis的安装配置,百度或谷歌即可,很简单. 首先,我来说说redis的应用场景,大部分公司都是将redis作为缓存服务器,或者作为ELK日志收集里面的缓存角色(其他这里就不做介绍,比 ...

  9. CSS------Filter属性的使用方法

    转载: http://www.w3cplus.com/css3/ten-effects-with-css3-filter

  10. Windows下CRF++进行中文人名识别的初次尝试

    语料来自1998年1月份人民日报语料 1 语料处理 1.1 原始语料数据格式 语料中,句子已经被分词好,并且在人名后以“/”标注了“nr”表示是人名,其他非人名的分词没有进行标注 1.2 CRF++要 ...