|
|
@@ -0,0 +1,233 @@
|
|
|
+//
|
|
|
+// RABadgeNumberView.m
|
|
|
+// Apex And Drivers
|
|
|
+//
|
|
|
+// Created by Jack on 2018/9/3.
|
|
|
+// Copyright © 2018年 USAI. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "RABadgeNumberView.h"
|
|
|
+
|
|
|
+#define Max_Width 30.0f
|
|
|
+#define Min_Width 16.0f
|
|
|
+#define Text_Size 12.0f
|
|
|
+
|
|
|
+@interface RABadgeNumberView ()
|
|
|
+{
|
|
|
+ UIColor *_backgroundColor;
|
|
|
+ UIColor *_foregroundColor;
|
|
|
+}
|
|
|
+@property (nonatomic,strong) CAShapeLayer *originLayer;
|
|
|
+@property (nonatomic,strong) CAShapeLayer *borderLayer;
|
|
|
+@property (nonatomic,strong) CATextLayer *valueLayer;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+
|
|
|
+@implementation RABadgeNumberView
|
|
|
+
|
|
|
+#pragma mark - Utils
|
|
|
+
|
|
|
++ (CGSize)sizeOfString:(NSString *)string font:(UIFont *)font{
|
|
|
+
|
|
|
+ if (string == nil) {
|
|
|
+ return CGSizeZero;
|
|
|
+ }
|
|
|
+
|
|
|
+ NSDictionary *attribute = @{NSFontAttributeName: font};
|
|
|
+
|
|
|
+ CGSize resSize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
|
|
|
+ options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
|
|
|
+ attributes:attribute
|
|
|
+ context:nil].size;
|
|
|
+
|
|
|
+ return resSize;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Public
|
|
|
+
|
|
|
+- (void)setBadgeNumber:(NSUInteger)badgeNumber {
|
|
|
+ _badgeNumber = badgeNumber;
|
|
|
+
|
|
|
+ self.hidden = _badgeNumber == 0;
|
|
|
+
|
|
|
+ NSString *text = nil;
|
|
|
+ if (_badgeNumber > 99) {
|
|
|
+ text = @"99+";
|
|
|
+ } else {
|
|
|
+ text = [NSString stringWithFormat:@"%lu",badgeNumber];
|
|
|
+ }
|
|
|
+
|
|
|
+ [self.valueLayer setString:text];
|
|
|
+
|
|
|
+ [self updateUI];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setForegroundColor:(UIColor *)foregroundColor {
|
|
|
+ _foregroundColor = foregroundColor;
|
|
|
+ self.valueLayer.foregroundColor = _foregroundColor.CGColor;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setBackgroundColor:(UIColor *)backgroundColor {
|
|
|
+ _backgroundColor = backgroundColor;
|
|
|
+ [self updateColor];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Private
|
|
|
+
|
|
|
+- (UIColor *)foregroundColor {
|
|
|
+ if (_foregroundColor == nil) {
|
|
|
+ _foregroundColor = [UIColor whiteColor];
|
|
|
+ }
|
|
|
+ return _foregroundColor;
|
|
|
+}
|
|
|
+
|
|
|
+- (UIColor *)backgroundColor {
|
|
|
+ if (_backgroundColor == nil) {
|
|
|
+ _backgroundColor = [UIColor redColor];
|
|
|
+ }
|
|
|
+ return _backgroundColor;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)willMoveToSuperview:(UIView *)newSuperview {
|
|
|
+
|
|
|
+ [super willMoveToSuperview:newSuperview];
|
|
|
+
|
|
|
+ [self.layer addSublayer:self.originLayer];
|
|
|
+ [self.layer addSublayer:self.borderLayer];
|
|
|
+ [self.layer addSublayer:self.valueLayer];
|
|
|
+
|
|
|
+ [self updateUI];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)layoutSubviews {
|
|
|
+ [super layoutSubviews];
|
|
|
+
|
|
|
+ [self updateUI];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark Frame
|
|
|
+
|
|
|
+- (void)setWidth:(CGFloat)width {
|
|
|
+
|
|
|
+ NSLayoutConstraint *widthConstraint = nil;
|
|
|
+ for (NSLayoutConstraint *constraint in self.constraints) {
|
|
|
+ if (constraint.firstAttribute == NSLayoutAttributeWidth) {
|
|
|
+ widthConstraint = constraint;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (widthConstraint) {
|
|
|
+ widthConstraint.constant = width;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ CGRect bounds = self.bounds;
|
|
|
+ bounds.size.width = width;
|
|
|
+ self.bounds = bounds;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setHeight:(CGFloat)height {
|
|
|
+
|
|
|
+ NSLayoutConstraint *heightConstraint = nil;
|
|
|
+ for (NSLayoutConstraint *constraint in self.constraints) {
|
|
|
+ if (constraint.firstAttribute == NSLayoutAttributeHeight) {
|
|
|
+ heightConstraint = constraint;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (heightConstraint) {
|
|
|
+ heightConstraint.constant = height;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ CGRect bounds = self.bounds;
|
|
|
+ bounds.size.height = height;
|
|
|
+ self.bounds = bounds;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)updateUI {
|
|
|
+
|
|
|
+ CGSize size = [self.class sizeOfString:self.valueLayer.string font:[UIFont systemFontOfSize:Text_Size]];
|
|
|
+ CGSize textSize = size;
|
|
|
+ if (size.width < Min_Width) {
|
|
|
+ size.width = Min_Width;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (size.height < Min_Width) {
|
|
|
+ size.height = Min_Width;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (size.height > size.width) {
|
|
|
+ size.width = size.height;
|
|
|
+ }
|
|
|
+
|
|
|
+ size.width += 5.0f;
|
|
|
+ size.height += 5.0f;
|
|
|
+
|
|
|
+ CGFloat r = size.height * 0.5;
|
|
|
+
|
|
|
+ // 更新自身Size
|
|
|
+ if (self.badgeNumber < 10) {
|
|
|
+ [self setWidth:size.width];
|
|
|
+ } else {
|
|
|
+ [self setWidth:size.width + 5];
|
|
|
+ }
|
|
|
+ [self setHeight:size.height];
|
|
|
+
|
|
|
+ // drag layer
|
|
|
+ self.borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:r].CGPath;
|
|
|
+
|
|
|
+ // 更新TextLayer Frame
|
|
|
+ CGFloat x = (CGRectGetWidth(self.bounds) - textSize.width) * 0.5,y = (CGRectGetHeight(self.bounds) - textSize.height) * 0.5;
|
|
|
+ self.valueLayer.frame = CGRectMake(x, y, textSize.width, textSize.height);
|
|
|
+}
|
|
|
+
|
|
|
+- (void)updateColor {
|
|
|
+ self.originLayer.fillColor = self.backgroundColor.CGColor;
|
|
|
+ self.originLayer.strokeColor = self.backgroundColor.CGColor;
|
|
|
+ self.borderLayer.fillColor = self.backgroundColor.CGColor;
|
|
|
+ self.borderLayer.strokeColor = self.backgroundColor.CGColor;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark Layer
|
|
|
+
|
|
|
+- (CAShapeLayer *)originLayer {
|
|
|
+ if (!_originLayer) {
|
|
|
+ _originLayer = [CAShapeLayer layer];
|
|
|
+ _originLayer.fillColor = self.backgroundColor.CGColor;
|
|
|
+ _originLayer.strokeColor = self.backgroundColor.CGColor;
|
|
|
+ _originLayer.lineWidth = 0.1f;
|
|
|
+ }
|
|
|
+ return _originLayer;
|
|
|
+}
|
|
|
+
|
|
|
+- (CAShapeLayer *)borderLayer {
|
|
|
+ if (!_borderLayer) {
|
|
|
+ _borderLayer = [CAShapeLayer layer];
|
|
|
+ _borderLayer.fillColor = self.backgroundColor.CGColor;
|
|
|
+ _borderLayer.strokeColor = self.backgroundColor.CGColor;
|
|
|
+ _borderLayer.lineWidth = 0.1f;
|
|
|
+ }
|
|
|
+ return _borderLayer;
|
|
|
+}
|
|
|
+
|
|
|
+- (CATextLayer *)valueLayer {
|
|
|
+ if (!_valueLayer) {
|
|
|
+ _valueLayer = [CATextLayer layer];
|
|
|
+ _valueLayer.foregroundColor = self.foregroundColor.CGColor;
|
|
|
+ _valueLayer.contentsScale = [UIScreen mainScreen].scale;
|
|
|
+ _valueLayer.alignmentMode = kCAAlignmentCenter;
|
|
|
+ _valueLayer.contentsGravity = kCAGravityCenter;
|
|
|
+
|
|
|
+ UIFont *font = [UIFont systemFontOfSize:Text_Size];
|
|
|
+ CGFontRef fontRef = CGFontCreateWithFontName((__bridge_retained CFStringRef)font.fontName);
|
|
|
+ _valueLayer.font = fontRef;
|
|
|
+ _valueLayer.fontSize = font.pointSize;
|
|
|
+ CGFontRelease(fontRef);
|
|
|
+ }
|
|
|
+ return _valueLayer;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+@end
|