BadgeValueView,iosbadgevalue

來源:互聯網
上載者:User

BadgeValueView,iosbadgevalue

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     = 10;        self.fixedHeight        = 20;        self.sensitiveTextWidth = 4;        self.position           = BadgePositionTopRight;        self.font               = [UIFont systemFontOfSize:12.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 / 2.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 == 0 ? 0 : 1;        }];            } else {            self.alpha = badgeValue.length == 0 ? 0 : 1;    }        // 如果值為空白,則不執行後續操作    if (badgeValue.length <= 0) {                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 / 2.f;    self.position == BadgePositionCenterLeft  ? self.left = -offset, self.centerY = self.contentView.middleY : 0;    self.position == BadgePositionCenterRight ? self.left = self.contentView.width - offset, self.centerY = self.contentView.middleY : 0;        self.position == BadgePositionTopLeft     ? self.left = -offset, self.y    = -offset : 0;    self.position == BadgePositionTopRight    ? self.top  = -offset, self.left = self.contentView.width - offset : 0;        self.position == BadgePositionBottomLeft  ? self.left = -offset, self.top = self.contentView.height - offset : 0;    self.position == BadgePositionBottomRight ? self.left = self.contentView.width - offset, self.top = self.contentView.height - offset : 0;}- (void)setBadgeValue:(NSString *)badgeValue {    [self setBadgeValue:badgeValue animated:NO];}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.