UITableViewCell+cornerRadius.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // UITableViewCell+cornerRadius.m
  3. // SmartLightForBigFish
  4. //
  5. // Created by coderYK on 2017/11/16.
  6. // Copyright © 2017年 RD. All rights reserved.
  7. //
  8. #import "UITableViewCell+cornerRadius.h"
  9. @implementation UITableViewCell (cornerRadius)
  10. - (void)rds_setCornerRadius:(CGFloat)radius atTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath {
  11. if ([self respondsToSelector:@selector(tintColor)]) {
  12. CGFloat cornerRadius = radius;
  13. self.backgroundColor = UIColor.clearColor;
  14. CAShapeLayer *layer = [[CAShapeLayer alloc] init];
  15. CGMutablePathRef pathRef = CGPathCreateMutable();
  16. CGRect bounds = CGRectInset(self.bounds, 0, 0);
  17. BOOL addLine = NO;
  18. if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  19. // 当前section只有一行
  20. CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
  21. } else if (indexPath.row == 0) {
  22. // 在第一行设置上圆角
  23. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
  24. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
  25. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  26. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
  27. addLine = YES;
  28. } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  29. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
  30. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
  31. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  32. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
  33. } else {
  34. CGPathAddRect(pathRef, nil, bounds);
  35. addLine = YES;
  36. }
  37. layer.path = pathRef;
  38. CFRelease(pathRef);
  39. //颜色修改
  40. layer.fillColor = [UIColor whiteColor].CGColor;
  41. layer.strokeColor = tableView.separatorColor.CGColor;
  42. if (addLine == YES) {
  43. CALayer *lineLayer = [[CALayer alloc] init];
  44. CGFloat lineHeight = .3f;
  45. lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
  46. lineLayer.backgroundColor = tableView.separatorColor.CGColor;
  47. [layer addSublayer:lineLayer];
  48. }
  49. UIView *testView = [[UIView alloc] initWithFrame:bounds];
  50. [testView.layer insertSublayer:layer atIndex:0];
  51. testView.backgroundColor = UIColor.clearColor;
  52. self.backgroundView = testView;
  53. }
  54. }
  55. - (void)rds_setBottomCornerRadius:(CGFloat)radius atTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath {
  56. if ([self respondsToSelector:@selector(tintColor)]) {
  57. CGFloat cornerRadius = radius;
  58. self.backgroundColor = UIColor.clearColor;
  59. CAShapeLayer *layer = [[CAShapeLayer alloc] init];
  60. CGMutablePathRef pathRef = CGPathCreateMutable();
  61. CGRect bounds = CGRectInset(self.bounds, 0, 0);
  62. BOOL addLine = NO;
  63. if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  64. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
  65. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
  66. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  67. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
  68. addLine = YES;
  69. } else {
  70. CGPathAddRect(pathRef, nil, bounds);
  71. addLine = YES;
  72. }
  73. layer.path = pathRef;
  74. CFRelease(pathRef);
  75. //颜色修改
  76. layer.fillColor = [UIColor whiteColor].CGColor;
  77. layer.strokeColor = tableView.separatorColor.CGColor;
  78. if (addLine == YES) {
  79. CALayer *lineLayer = [[CALayer alloc] init];
  80. CGFloat lineHeight = .3f;
  81. lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10.6, bounds.size.height-lineHeight, bounds.size.width-10.6, lineHeight);
  82. lineLayer.backgroundColor = tableView.separatorColor.CGColor;
  83. [layer addSublayer:lineLayer];
  84. }
  85. UIView *testView = [[UIView alloc] initWithFrame:bounds];
  86. [testView.layer insertSublayer:layer atIndex:0];
  87. testView.backgroundColor = UIColor.clearColor;
  88. self.backgroundView = testView;
  89. }
  90. }
  91. - (void)rds_addSeparatorForCellAtFirstOrBottomColor:(UIColor *)separatorColor atTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath {
  92. if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  93. // 当前section只有一行
  94. } else if (indexPath.row == 0) {
  95. // 在第一行设置上圆角
  96. } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  97. }
  98. }
  99. @end