LHYLinesPaoPaoView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // YJYLinesPaoPaoView.m
  3. // YJYLinesView
  4. //
  5. // Created by lihaiyang on 2018/10/23.
  6. // Copyright © 2018 lihaiyang. All rights reserved.
  7. //
  8. #import "LHYLinesPaoPaoView.h"
  9. #import "LinesSelectCell.h"
  10. @interface LHYLinesPaoPaoView()<UITableViewDelegate,UITableViewDataSource>
  11. @property (nonatomic,strong) UITableView *listTb;
  12. @property (assign, nonatomic) CGFloat maxLeftTitleWidth;
  13. @property (assign, nonatomic) CGFloat maxRightTitleWidth;
  14. /** 遮罩 */
  15. @property (nonatomic, strong) CAShapeLayer *maskLayer;
  16. /** 路径 */
  17. @property (nonatomic, strong) UIBezierPath *borderPath;
  18. @end
  19. @implementation LHYLinesPaoPaoView {
  20. NSArray *_dataArr;
  21. NSArray *_colorArr;
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. self.backgroundColor = [UIColor clearColor];
  27. _beyondLeft = NO;
  28. _beyondRight = NO;
  29. [self setUp];
  30. }
  31. return self;
  32. }
  33. - (instancetype)init {
  34. self = [super init];
  35. if (self) {
  36. [self setUp];
  37. }
  38. return self;
  39. }
  40. - (void)setUp {
  41. UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
  42. bgImageView.backgroundColor = [UIColor clearColor];
  43. [self addSubview:bgImageView];
  44. [self addSubview:self.listTb];
  45. }
  46. -(void)setFrame:(CGRect)frame{
  47. super.frame = frame;
  48. [self.listTb setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
  49. }
  50. - (void)show:(NSArray *)dataArr and:(NSString *)title andTitleColor:(UIColor *)titleColor colorArr:(NSArray *)color {
  51. self.maxLeftTitleWidth = 0;
  52. NSMutableArray *titleArray = [NSMutableArray array];
  53. for (int i = 0; i < dataArr.count; i++) {
  54. id obj = dataArr[i];
  55. NSMutableArray * marr = [NSMutableArray array];
  56. if ([obj isKindOfClass:[NSString class]]) {
  57. NSString *dataTitle = dataArr[i];
  58. float titleWidth= 0;
  59. titleWidth = [LHYLinesPaoPaoView measureSinglelineStringWidth:dataTitle andFont:[UIFont systemFontOfSize:13]];
  60. self.maxLeftTitleWidth = titleWidth + 5 > self.maxLeftTitleWidth ? titleWidth + 5 : self.maxLeftTitleWidth;
  61. [marr addObject:dataTitle];
  62. } else if([obj isKindOfClass:[NSArray class]]) {
  63. NSArray * arr = obj;
  64. for (int o = 0 ; o < arr.count ; o++) {
  65. NSString * dataTitle = arr[o];
  66. float titleWidth= 0;
  67. if (o == 0) {
  68. titleWidth = [LHYLinesPaoPaoView measureSinglelineStringWidth:dataTitle andFont:[UIFont systemFontOfSize:13]];
  69. self.maxLeftTitleWidth = titleWidth + 5 > self.maxLeftTitleWidth ? titleWidth + 5 : self.maxLeftTitleWidth;
  70. } else {
  71. titleWidth = [LHYLinesPaoPaoView measureSinglelineStringWidth:dataTitle andFont:[UIFont systemFontOfSize:13]];
  72. self.maxRightTitleWidth = titleWidth + 5 > self.maxRightTitleWidth ? titleWidth + 5 : self.maxRightTitleWidth;
  73. }
  74. [marr addObject:dataTitle];
  75. }
  76. }
  77. [titleArray addObject:marr];
  78. }
  79. _dataArr = titleArray;
  80. _colorArr = color;
  81. if (title && title.length > 0) {
  82. UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 23.5)];
  83. UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 6.5, CGRectGetWidth(view.frame) - 40, 17)];
  84. titleLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
  85. titleLab.textColor = [UIColor colorWithHexString:@"#d3d3d3"];
  86. titleLab.text = title;
  87. titleLab.textAlignment = NSTextAlignmentCenter;
  88. titleLab.textColor = titleColor;
  89. [view addSubview:titleLab];
  90. self.listTb.tableHeaderView = view;
  91. } else {
  92. UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 6.5)];
  93. self.listTb.tableHeaderView = view;
  94. }
  95. self.listTb.rowHeight = 17;
  96. [self.listTb reloadData];
  97. }
  98. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  99. return _dataArr.count;
  100. }
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. LinesSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LinesSelectCell"];
  103. if (!cell) {
  104. cell = [[LinesSelectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LinesSelectCell"];
  105. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  106. cell.leftWidth = self.maxLeftTitleWidth;
  107. cell.rightWith = self.maxRightTitleWidth;
  108. }
  109. NSArray * arr = _dataArr[indexPath.row];
  110. if (arr.count == 1) {
  111. [cell showTitle:arr[0] andTitleColor:_colorArr[indexPath.row] andTitleFont:[UIFont fontWithName:@"Helvetica-Bold" size:13] andDescTile:nil andDescTileColor:nil andDescTitleFont:nil];
  112. } else if (arr.count == 2) {
  113. [cell showTitle:arr[0] andTitleColor:_colorArr[indexPath.row] andTitleFont:[UIFont fontWithName:@"Helvetica-Bold" size:13] andDescTile:arr[1] andDescTileColor:_colorArr[indexPath.row] andDescTitleFont:[UIFont systemFontOfSize:12]];
  114. }
  115. return cell;
  116. }
  117. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  118. return 17;
  119. }
  120. + (CGSize)getWidthAndHeight:(NSArray *)dataArray {
  121. CGFloat width = 0;
  122. CGFloat height = 0;
  123. if (dataArray.count > 0) height = 17 * (dataArray.count + 1);
  124. for (int i = 0; i < dataArray.count; i++) {
  125. id obj = dataArray[i];
  126. if ([obj isKindOfClass:[NSString class]]) {
  127. float sizeWidth = [LHYLinesPaoPaoView measureSinglelineStringWidth:(NSString *)obj andFont:[UIFont systemFontOfSize:13]];
  128. sizeWidth = sizeWidth + 5 < 80 ? 80 : sizeWidth + 5;
  129. width = sizeWidth > width ? sizeWidth : width;
  130. }else if ([obj isKindOfClass:[NSArray class]]){
  131. NSArray * arr = obj;
  132. if (arr.count > 0) width += (arr.count - 1) * 10;
  133. for (int i = 0; i < arr.count; i++) {
  134. float sizeWidth = [LHYLinesPaoPaoView measureSinglelineStringWidth:(NSString *)obj andFont:[UIFont systemFontOfSize:13]];
  135. sizeWidth = sizeWidth + 5 < 80 ? 80 : sizeWidth + 5;
  136. width = sizeWidth > width ? sizeWidth : width;
  137. }
  138. }
  139. }
  140. if (width < 80) width = 80;
  141. width += 20;
  142. return CGSizeMake(width, height);
  143. }
  144. - (UITableView *)listTb {
  145. if (!_listTb) {
  146. _listTb = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)) style:UITableViewStylePlain];
  147. _listTb.backgroundColor = [UIColor clearColor];
  148. _listTb.separatorStyle = UITableViewCellSeparatorStyleNone;
  149. _listTb.allowsSelection = NO;
  150. _listTb.bounces = NO;
  151. _listTb.dataSource = self;
  152. _listTb.delegate = self;
  153. }
  154. return _listTb;
  155. }
  156. #pragma mark ---------画边框并填充颜色-------------
  157. -(void)drawBoxWithDirection:(Direction)direction{
  158. UIView * view = [[UIView alloc] initWithFrame:self.bounds];
  159. view.backgroundColor = self.backgroundColor;
  160. self.backgroundColor = [UIColor clearColor];
  161. [self addSubview:view];
  162. [self bringSubviewToFront:self.listTb];
  163. // 初始化遮罩
  164. self.maskLayer = [CAShapeLayer layer];
  165. // 设置遮罩
  166. [view.layer setMask:self.maskLayer];
  167. // 初始化路径
  168. self.borderPath = [UIBezierPath bezierPath];
  169. // 遮罩层frame
  170. self.maskLayer.frame = view.bounds;
  171. CGFloat pathX = 0;
  172. if (_beyondLeft) {
  173. pathX = _pointX;
  174. } else if (_beyondRight) {
  175. pathX = self.bounds.size.width - (self.chartContentWidth - _pointX);
  176. } else {
  177. pathX = self.bounds.size.width/2;
  178. }
  179. if (direction == directionTop) {
  180. CGRect frame = self.listTb.frame;
  181. frame.origin.y = 0;
  182. [self.listTb setFrame:frame];
  183. /// 设置path起点
  184. [self.borderPath moveToPoint:CGPointMake(5, 5)];
  185. /// 左上角的圆角
  186. [self.borderPath addQuadCurveToPoint:CGPointMake(10, 0) controlPoint:CGPointMake(5, 0)];
  187. /// 直线,到右上角
  188. [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width - 10, 0)];
  189. /// 右上角的圆角
  190. [self.borderPath addQuadCurveToPoint:CGPointMake(self.bounds.size.width -5, 5) controlPoint:CGPointMake(self.bounds.size.width - 5, 0)];
  191. /// 直线,到右下角
  192. [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width - 5, self.bounds.size.height - 10)];
  193. /// 右下角的圆角
  194. [self.borderPath addQuadCurveToPoint:CGPointMake(self.bounds.size.width - 10, self.bounds.size.height - 5) controlPoint:CGPointMake(self.bounds.size.width - 5, self.bounds.size.height - 5)];
  195. /// 底部的小三角形
  196. [self.borderPath addLineToPoint:CGPointMake(pathX + 5, self.bounds.size.height - 5)];
  197. [self.borderPath addLineToPoint:CGPointMake(pathX, self.bounds.size.height)];
  198. [self.borderPath addLineToPoint:CGPointMake(pathX - 5, self.bounds.size.height - 5)];
  199. /// 直线,到左下角
  200. [self.borderPath addLineToPoint:CGPointMake(10, self.bounds.size.height - 5)];
  201. /// 左下角的圆角
  202. [self.borderPath addQuadCurveToPoint:CGPointMake(5, self.bounds.size.height - 10) controlPoint:CGPointMake(5, self.bounds.size.height - 5)];
  203. /// 直线,回到起点
  204. [self.borderPath addLineToPoint:CGPointMake(5, 5)];
  205. }else{
  206. CGRect frame = self.listTb.frame;
  207. frame.origin.y = 5;
  208. [self.listTb setFrame:frame];
  209. /// 设置path起点
  210. [self.borderPath moveToPoint:CGPointMake(5, 10)];
  211. /// 左上角的圆角
  212. [self.borderPath addQuadCurveToPoint:CGPointMake(10, 5) controlPoint:CGPointMake(5, 5)];
  213. /// 顶部的小三角形
  214. [self.borderPath addLineToPoint:CGPointMake(pathX - 5, 5)];
  215. [self.borderPath addLineToPoint:CGPointMake(pathX, 0)];
  216. [self.borderPath addLineToPoint:CGPointMake(pathX + 5, 5)];
  217. /// 直线,到右上角
  218. [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width - 10, 5)];
  219. /// 右上角的圆角
  220. [self.borderPath addQuadCurveToPoint:CGPointMake(self.bounds.size.width - 5, 10) controlPoint:CGPointMake(self.bounds.size.width - 5, 5)];
  221. /// 直线,到右下角
  222. [self.borderPath addLineToPoint:CGPointMake(self.bounds.size.width - 5, self.bounds.size.height - 5)];
  223. /// 右下角的圆角
  224. [self.borderPath addQuadCurveToPoint:CGPointMake(self.bounds.size.width - 10, self.bounds.size.height) controlPoint:CGPointMake(self.bounds.size.width - 5, self.bounds.size.height)];
  225. /// 直线,到左下角
  226. [self.borderPath addLineToPoint:CGPointMake(10, self.bounds.size.height)];
  227. /// 左下角的圆角
  228. [self.borderPath addQuadCurveToPoint:CGPointMake(5, self.bounds.size.height - 5) controlPoint:CGPointMake(5, self.bounds.size.height)];
  229. /// 直线,回到起点
  230. [self.borderPath addLineToPoint:CGPointMake(5, 10)];
  231. }
  232. /// 将这个path赋值给maskLayer的path
  233. self.maskLayer.path = self.borderPath.CGPath;
  234. }
  235. -(UIColor*)colorWithHexString:(NSString*)stringToConvert{
  236. if([stringToConvert hasPrefix:@"#"])
  237. {
  238. stringToConvert = [stringToConvert substringFromIndex:1];
  239. }
  240. NSScanner*scanner = [NSScanner scannerWithString:stringToConvert];
  241. unsigned hexNum;
  242. if(![scanner scanHexInt:&hexNum])
  243. {
  244. return nil;
  245. }
  246. return [UIColor colorWithRGBHex:hexNum];
  247. }
  248. - (UIColor *)colorWithRGBHex:(UInt32)hex {
  249. int r = (hex >> 16) & 0xFF;
  250. int g = (hex >> 8) & 0xFF;
  251. int b = (hex) & 0xFF;
  252. return [UIColor colorWithRed:r / 255.0f
  253. green:g / 255.0f
  254. blue:b / 255.0f
  255. alpha:1.0f];
  256. }
  257. + (float)measureSinglelineStringWidth:(NSString*)str andFont:(UIFont*)wordFont {
  258. if (str == nil) return 0;
  259. CGSize measureSize = [str boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;
  260. return ceil(measureSize.width);
  261. }
  262. @end