BadgeValueView, iosbadgevalue

Source: Internet
Author: User

BadgeValueView, iosbadgevalue

BadgeValueView

 

Effect

 

Source code

BadgeValueView in https://github.com/YouXianMing/UI-Component-Collection

//// 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, locate, BadgePositionBottomLeft, BadgePositionBottomRight,}; @ interface BadgeValueView: UIView/*** bedge value */@ property (nonatomic, strong) NSString * badgeValue;/*** attached view */@ property (nonatomic, weak) UIView * contentView;/*** increase the width of sensitive characters. The default value is 4 */@ property (nonatomic) CGFloat sensitiveTextWidth;/*** increase the width of sensitive characters, the default value is 10 */@ property (nonatomic) CGFloat sensitiveWidth;/*** fixed height. The default value is 20 */@ property (nonatomic) CGFloat fixedHeight./*** location information, the default value is BadgePositionTopRight */@ property (nonatomic) BadgePosition position;/*** font. The default value is 12 */@ property (nonatomic, strong) UIFont * font; /*** font color, white by default */@ property (nonatomic, strong) UIColor * textColor;/*** bedge color, red by default */@ property (nonatomic, strong) UIColor * badgeColor;/*** start to take effect */-(void) makeEffect; /*** set BadgeValue ** @ param value BadgeValue * @ param animated whether to execute an animation */-(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 systemFontOfSi Ze: 12.f]; self. textColor = [UIColor whiteColor]; self. badgeColor = [UIColor redColor];} return self;}-(void) makeEffect {// tag self. label = [[UILabel alloc] init]; self. label. textColor = self. textColor; self. label. textAlignment = NSTextAlignmentCenter; self. label. font = self. font; [self addSubview: self. label]; // the background color self. backgroundColor = self. badgeColor; self. width = self. fixedHeight; self. hei Ght = 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; // whether to execute the animation if (animated) {[UIView animateWithDuration: 0.15f animations: ^ {self. alpha = badgeValue. length = 0? 0: 1 ;}] ;}else {self. alpha = badgeValue. length = 0? 0: 1;} // if the value is null, if (badgeValue. length <= 0) {return;} // sets the text self. label. text = badgeValue; [self. label sizeToFit]; // update the size if (self. label. width + self. sensitiveTextWidth> self. width) {self. width + = self. sensitiveWidth;} else {self. width = self. fixedHeight;} // update the text size self. label. center = self. middlePoint; // update the size CGFloat offset = self according to the position. fixedHeight/2.f; self. position = Badge PositionCenterLeft? 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.