// // IFishActivityCateView.m // Ifish // // Created by imac on 2017/5/12. // Copyright © 2017年 lianlian. All rights reserved. // #import "IFishActivityCateView.h" @interface IFishActivityCateView () @property (nonatomic,strong) UILabel *textLabe; @property (nonatomic,strong) UIView *circleView; @end @implementation IFishActivityCateView -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self creatSub]; } return self; } -(void)awakeFromNib{ [super awakeFromNib]; [self creatSub]; } -(void)layoutSubviews{ [super layoutSubviews]; } -(void)creatSub{ if (!_circleView) { _circleView = [[UIView alloc] init]; [self addSubview:_circleView]; } if (!_textLabe) { _textLabe = [[UILabel alloc] init]; _textLabe.textAlignment = NSTextAlignmentLeft; _textLabe.textColor = [UIColor whiteColor]; _textLabe.font = [UIFont systemFontOfSize:12]; [self addSubview:_textLabe]; } UIColor *color = [self loadrandomColor]; _textLabe.backgroundColor = color; _circleView.backgroundColor = color; } -(UIColor *)loadrandomColor { UIColor *color = nil; int i = (arc4random()%4) + 1;// 设置随机色 switch (i) { case 1: { color = RGB(113, 219, 254); } break; case 2: { color = RGB(255, 117, 156); } break; case 3: { color = RGB(255, 175, 117); } break; case 4: { color = RGB(117, 198, 255); } break; default: break; } return color; } -(void)activityViewaddTitle:(NSString *)title { _textLabe.text = title; CGFloat strW = [self widthForString:title fontSize:12 andHeight:self.frame.size.height]; CGFloat labeW= strW + 15; _textLabe.frame = CGRectMake(self.frame.size.width-labeW, 0, labeW, self.frame.size.height); _circleView.frame = CGRectMake(self.frame.size.width-labeW - self.frame.size.height/2,0, self.frame.size.height, self.frame.size.height); [self bringSubviewToFront:_textLabe]; _circleView.layer.masksToBounds = YES; _circleView.layer.cornerRadius = self.frame.size.height/2; } #pragma -mark -functions //获取字符串的宽度 -(float) widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height { CGRect rect = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil]; CGSize sizeToFit = rect.size; return sizeToFit.width; } //获得字符串的高度 -(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width { CGRect rect = [value boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil]; CGSize sizeToFit = rect.size; return sizeToFit.height; } @end