CustomStepProgressView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // CustomStepProgressView.m
  3. // shiku_im
  4. //
  5. // Created by 周西 on 17/5/19.
  6. // Copyright © 2017年 UAS. All rights reserved.
  7. //
  8. #import "CustomStepProgressView.h"
  9. //static NSInteger targetNum;
  10. #import <objc/runtime.h>
  11. @interface UILabel (subStr)
  12. @property(copy,nonatomic)NSString *subStr;
  13. @property(assign,nonatomic)BOOL isText;
  14. @end
  15. @implementation UILabel (subStr)
  16. -(void)setSubStr:(NSString *)subStr{
  17. objc_setAssociatedObject(self, @"UILabelSubStr", subStr,OBJC_ASSOCIATION_COPY_NONATOMIC);
  18. }
  19. -(NSString *)subStr{
  20. return objc_getAssociatedObject(self, @"UILabelSubStr");
  21. }
  22. -(void)setIsText:(BOOL)isText{
  23. NSNumber *number = isText ? @1:@0;
  24. objc_setAssociatedObject(self, @"UILabelSubStrIsText", number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  25. }
  26. -(BOOL)isText{
  27. NSNumber *number = objc_getAssociatedObject(self, @"UILabelSubStrIsText");
  28. return [number boolValue];
  29. }
  30. @end
  31. @interface CustomStepProgressView()
  32. @property(assign,nonatomic)BOOL isExist;
  33. @property(copy,nonatomic)NSString *preStr;
  34. @property(strong,nonatomic)NSMutableArray *tempArr;
  35. @end
  36. @implementation CustomStepProgressView
  37. {
  38. UILabel * _labl1;
  39. }
  40. - (instancetype)initWithFrame:(CGRect)frame
  41. {
  42. self = [super initWithFrame:frame];
  43. if (self) {
  44. self.isExist = NO;
  45. self.backgroundColor = [UIColor clearColor];
  46. self.tempArr = [@[@"test",@"test",@"test",@"test",@"test",@"test",@"test",@"test",@"test",@"test"] mutableCopy];;
  47. [self setUpUIWithArr:self.tempArr];
  48. }
  49. return self;
  50. }
  51. -(void)setCurrentProgressStr:(NSString *)currentProgressStr{
  52. _currentProgressStr = [currentProgressStr mutableCopy];
  53. [self changeState];
  54. if (![_preStr isEqualToString:currentProgressStr]) {
  55. _preStr = currentProgressStr;
  56. }
  57. }
  58. -(void)setTitleArr:(NSArray *)titleArr{
  59. _titleArr = titleArr;
  60. if (!_isExist) {//防止多次创建
  61. if(_tempArr.count != titleArr.count){//子控件数不同,需重新创建
  62. [self removeAllSubviews];
  63. [self setUpUIWithArr:_titleArr];
  64. self.isExist = YES;
  65. }else{//子控件数相同,只需重新赋值即可
  66. [self getNewValue];
  67. self.isExist = YES;
  68. }
  69. }
  70. }
  71. - (void)setUpUIWithArr:(NSArray *)arr{
  72. // rect.size.width = SCREEN_WIDTH;
  73. // 线宽
  74. CGFloat lineWidth = 2.0f;
  75. // 节点间线段长度
  76. CGFloat distanceBetweenTwoPoints = (self.frame.size.width-(arr.count*18)-4)/(arr.count-1);
  77. // 圆的直径
  78. CGFloat circleDiameter = 18;
  79. for (int i = 0; i<arr.count; i++) {
  80. UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(lineWidth+(18+distanceBetweenTwoPoints)*i, lineWidth/2, 18, 18)];
  81. lbl.text = [NSString stringWithFormat:@"%ld",(long)i+1];
  82. lbl.subStr = arr[i];
  83. lbl.font = FONT_SIZE(10);
  84. lbl.tag = 333 + i;
  85. lbl.textColor = [UIColor whiteColor];
  86. lbl.textAlignment = NSTextAlignmentCenter;
  87. lbl.layer.masksToBounds = YES;
  88. lbl.isText = NO;
  89. lbl.layer.cornerRadius = circleDiameter/2;
  90. [self addSubview:lbl];
  91. float width = self.frame.size.width/arr.count;
  92. UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(width*i, CGRectGetMaxY(lbl.frame)+1, width, 30)];
  93. lbl1.text = arr[i];
  94. lbl1.font = FONT_SIZE(9);
  95. lbl1.textAlignment = NSTextAlignmentCenter;
  96. lbl1.isText = YES;
  97. lbl1.tag = 666 + i;
  98. lbl1.numberOfLines = 0;
  99. [self addSubview:lbl1];
  100. NSString *str1 = [NSString stringWithFormat:@"%@",arr[i]];
  101. if ([str1 isEqualToString:_currentProgressStr]) {
  102. lbl1.textColor = [UIColor orangeColor];
  103. lbl.backgroundColor =[UIColor orangeColor];
  104. }
  105. else{
  106. lbl.backgroundColor = Color(172, 170, 171, 1);
  107. }
  108. }
  109. for (int i = 0; i<arr.count-1; i++) {
  110. UIImageView * ivLine = [[UIImageView alloc]initWithFrame:CGRectMake(lineWidth+18*(i+1)+distanceBetweenTwoPoints*i, 9, distanceBetweenTwoPoints, lineWidth)];
  111. ivLine.backgroundColor = Color(172, 170, 171, 1);
  112. [self addSubview:ivLine];
  113. }
  114. }
  115. /**
  116. 对子控件进行重新赋值
  117. */
  118. -(void)getNewValue{
  119. NSMutableArray *textArr = [NSMutableArray array];
  120. NSMutableArray *textNum = [NSMutableArray array];
  121. for (int i = 0; i < self.subviews.count; i++) {
  122. UIView *subView = self.subviews[i];
  123. if ([subView isKindOfClass:[UILabel class]]) {
  124. UILabel *label = (UILabel *)subView;
  125. if (label.isText) {
  126. [textArr addObject:label];
  127. }else{
  128. [textNum addObject:label];
  129. }
  130. }
  131. }
  132. for (int i = 0; i < _titleArr.count; i++) {
  133. UILabel *label = (UILabel *)textArr[i];
  134. label.text = _titleArr[i];
  135. UILabel *numLabel = (UILabel *)textNum[i];
  136. numLabel.subStr = _titleArr[i];
  137. }
  138. }
  139. -(void)changeState{
  140. for (int i = 0; i < self.subviews.count; i++) {
  141. UIView *subView = self.subviews[i];
  142. if ([subView isKindOfClass:[UILabel class]]) {
  143. UILabel *label = (UILabel *)subView;
  144. if ([label.text isEqualToString:_preStr]) {
  145. label.textColor = [UIColor blackColor];
  146. }else if([label.text isEqualToString:_currentProgressStr]){
  147. UILabel *label = (UILabel *)subView;
  148. label.textColor = [UIColor orangeColor];
  149. }
  150. }
  151. }
  152. for (int i = 0; i < _titleArr.count; i++) {
  153. UILabel *label = (UILabel *)[self viewWithTag:(333 + i)];
  154. if ([label.subStr isEqualToString:_preStr]) {
  155. label.textColor = [UIColor whiteColor];
  156. label.backgroundColor = Color(172, 170, 171, 1);
  157. }else if( [label.subStr isEqualToString:_currentProgressStr]){
  158. label.backgroundColor = [UIColor orangeColor];
  159. }
  160. }
  161. }
  162. @end