| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // CustomStepProgressView.m
- // shiku_im
- //
- // Created by 周西 on 17/5/19.
- // Copyright © 2017年 UAS. All rights reserved.
- //
- #import "CustomStepProgressView.h"
- //static NSInteger targetNum;
- #import <objc/runtime.h>
- @interface UILabel (subStr)
- @property(copy,nonatomic)NSString *subStr;
- @property(assign,nonatomic)BOOL isText;
- @end
- @implementation UILabel (subStr)
- -(void)setSubStr:(NSString *)subStr{
-
- objc_setAssociatedObject(self, @"UILabelSubStr", subStr,OBJC_ASSOCIATION_COPY_NONATOMIC);
- }
- -(NSString *)subStr{
- return objc_getAssociatedObject(self, @"UILabelSubStr");
- }
- -(void)setIsText:(BOOL)isText{
-
- NSNumber *number = isText ? @1:@0;
-
- objc_setAssociatedObject(self, @"UILabelSubStrIsText", number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
-
-
- }
- -(BOOL)isText{
-
- NSNumber *number = objc_getAssociatedObject(self, @"UILabelSubStrIsText");
-
- return [number boolValue];
- }
- @end
- @interface CustomStepProgressView()
- @property(assign,nonatomic)BOOL isExist;
- @property(copy,nonatomic)NSString *preStr;
- @property(strong,nonatomic)NSMutableArray *tempArr;
- @end
- @implementation CustomStepProgressView
- {
- UILabel * _labl1;
-
- }
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- self.isExist = NO;
-
- self.backgroundColor = [UIColor clearColor];
-
- self.tempArr = [@[@"test",@"test",@"test",@"test",@"test",@"test",@"test",@"test",@"test",@"test"] mutableCopy];;
-
- [self setUpUIWithArr:self.tempArr];
-
- }
- return self;
- }
- -(void)setCurrentProgressStr:(NSString *)currentProgressStr{
-
-
- _currentProgressStr = [currentProgressStr mutableCopy];
-
- [self changeState];
-
- if (![_preStr isEqualToString:currentProgressStr]) {
-
- _preStr = currentProgressStr;
-
- }
-
- }
- -(void)setTitleArr:(NSArray *)titleArr{
-
- _titleArr = titleArr;
-
- if (!_isExist) {//防止多次创建
-
- if(_tempArr.count != titleArr.count){//子控件数不同,需重新创建
- [self removeAllSubviews];
- [self setUpUIWithArr:_titleArr];
- self.isExist = YES;
-
- }else{//子控件数相同,只需重新赋值即可
-
- [self getNewValue];
-
- self.isExist = YES;
- }
-
- }
-
-
-
- }
- - (void)setUpUIWithArr:(NSArray *)arr{
- // rect.size.width = SCREEN_WIDTH;
- // 线宽
-
- CGFloat lineWidth = 2.0f;
- // 节点间线段长度
- CGFloat distanceBetweenTwoPoints = (self.frame.size.width-(arr.count*18)-4)/(arr.count-1);
- // 圆的直径
- CGFloat circleDiameter = 18;
- for (int i = 0; i<arr.count; i++) {
- UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(lineWidth+(18+distanceBetweenTwoPoints)*i, lineWidth/2, 18, 18)];
-
- lbl.text = [NSString stringWithFormat:@"%ld",(long)i+1];
- lbl.subStr = arr[i];
- lbl.font = FONT_SIZE(10);
- lbl.tag = 333 + i;
- lbl.textColor = [UIColor whiteColor];
- lbl.textAlignment = NSTextAlignmentCenter;
- lbl.layer.masksToBounds = YES;
- lbl.isText = NO;
- lbl.layer.cornerRadius = circleDiameter/2;
- [self addSubview:lbl];
- float width = self.frame.size.width/arr.count;
- UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(width*i, CGRectGetMaxY(lbl.frame)+1, width, 30)];
- lbl1.text = arr[i];
- lbl1.font = FONT_SIZE(9);
- lbl1.textAlignment = NSTextAlignmentCenter;
- lbl1.isText = YES;
- lbl1.tag = 666 + i;
- lbl1.numberOfLines = 0;
- [self addSubview:lbl1];
-
- NSString *str1 = [NSString stringWithFormat:@"%@",arr[i]];
- if ([str1 isEqualToString:_currentProgressStr]) {
-
- lbl1.textColor = [UIColor orangeColor];
- lbl.backgroundColor =[UIColor orangeColor];
- }
- else{
- lbl.backgroundColor = Color(172, 170, 171, 1);
- }
- }
- for (int i = 0; i<arr.count-1; i++) {
- UIImageView * ivLine = [[UIImageView alloc]initWithFrame:CGRectMake(lineWidth+18*(i+1)+distanceBetweenTwoPoints*i, 9, distanceBetweenTwoPoints, lineWidth)];
- ivLine.backgroundColor = Color(172, 170, 171, 1);
-
- [self addSubview:ivLine];
-
- }
-
- }
- /**
- 对子控件进行重新赋值
- */
- -(void)getNewValue{
-
- NSMutableArray *textArr = [NSMutableArray array];
- NSMutableArray *textNum = [NSMutableArray array];
- for (int i = 0; i < self.subviews.count; i++) {
-
- UIView *subView = self.subviews[i];
-
- if ([subView isKindOfClass:[UILabel class]]) {
- UILabel *label = (UILabel *)subView;
-
- if (label.isText) {
-
- [textArr addObject:label];
-
- }else{
-
- [textNum addObject:label];
-
- }
- }
- }
-
- for (int i = 0; i < _titleArr.count; i++) {
-
- UILabel *label = (UILabel *)textArr[i];
- label.text = _titleArr[i];
- UILabel *numLabel = (UILabel *)textNum[i];
- numLabel.subStr = _titleArr[i];
- }
-
- }
- -(void)changeState{
-
- for (int i = 0; i < self.subviews.count; i++) {
-
- UIView *subView = self.subviews[i];
-
- if ([subView isKindOfClass:[UILabel class]]) {
- UILabel *label = (UILabel *)subView;
- if ([label.text isEqualToString:_preStr]) {
-
- label.textColor = [UIColor blackColor];
-
- }else if([label.text isEqualToString:_currentProgressStr]){
-
- UILabel *label = (UILabel *)subView;
-
- label.textColor = [UIColor orangeColor];
-
- }
-
- }
-
-
- }
-
- for (int i = 0; i < _titleArr.count; i++) {
- UILabel *label = (UILabel *)[self viewWithTag:(333 + i)];
- if ([label.subStr isEqualToString:_preStr]) {
-
- label.textColor = [UIColor whiteColor];
- label.backgroundColor = Color(172, 170, 171, 1);
-
- }else if( [label.subStr isEqualToString:_currentProgressStr]){
-
- label.backgroundColor = [UIColor orangeColor];
-
- }
-
- }
- }
- @end
|