JXCategoryTitleView.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // JXCategoryView.m
  3. // UI系列测试
  4. //
  5. // Created by jiaxin on 2018/3/15.
  6. // Copyright © 2018年 jiaxin. All rights reserved.
  7. //
  8. #import "JXCategoryTitleView.h"
  9. #import "JXCategoryFactory.h"
  10. @interface JXCategoryTitleView ()
  11. @end
  12. @implementation JXCategoryTitleView
  13. - (void)initializeData
  14. {
  15. [super initializeData];
  16. _titleNumberOfLines = 1;
  17. _titleLabelZoomEnabled = NO;
  18. _titleLabelZoomScale = 1.2;
  19. _titleColor = [UIColor blackColor];
  20. _titleSelectedColor = [UIColor redColor];
  21. _titleFont = [UIFont systemFontOfSize:15];
  22. _titleColorGradientEnabled = NO;
  23. _titleLabelMaskEnabled = NO;
  24. _titleLabelZoomScrollGradientEnabled = YES;
  25. _titleLabelStrokeWidthEnabled = NO;
  26. _titleLabelSelectedStrokeWidth = -3;
  27. _titleLabelVerticalOffset = 0;
  28. _titleLabelAnchorPointStyle = JXCategoryTitleLabelAnchorPointStyleCenter;
  29. }
  30. - (UIFont *)titleSelectedFont {
  31. if (_titleSelectedFont != nil) {
  32. return _titleSelectedFont;
  33. }
  34. return self.titleFont;
  35. }
  36. #pragma mark - Override
  37. - (Class)preferredCellClass {
  38. return [JXCategoryTitleCell class];
  39. }
  40. - (void)refreshDataSource {
  41. NSMutableArray *tempArray = [NSMutableArray array];
  42. for (int i = 0; i < self.titles.count; i++) {
  43. JXCategoryTitleCellModel *cellModel = [[JXCategoryTitleCellModel alloc] init];
  44. [tempArray addObject:cellModel];
  45. }
  46. self.dataSource = tempArray;
  47. }
  48. - (void)refreshSelectedCellModel:(JXCategoryBaseCellModel *)selectedCellModel unselectedCellModel:(JXCategoryBaseCellModel *)unselectedCellModel {
  49. [super refreshSelectedCellModel:selectedCellModel unselectedCellModel:unselectedCellModel];
  50. JXCategoryTitleCellModel *myUnselectedCellModel = (JXCategoryTitleCellModel *)unselectedCellModel;
  51. myUnselectedCellModel.titleCurrentColor = myUnselectedCellModel.titleNormalColor;
  52. myUnselectedCellModel.titleLabelCurrentZoomScale = myUnselectedCellModel.titleLabelNormalZoomScale;
  53. myUnselectedCellModel.titleLabelCurrentStrokeWidth = myUnselectedCellModel.titleLabelNormalStrokeWidth;
  54. JXCategoryTitleCellModel *myselectedCellModel = (JXCategoryTitleCellModel *)selectedCellModel;
  55. myselectedCellModel.titleCurrentColor = myUnselectedCellModel.titleSelectedColor;
  56. myselectedCellModel.titleLabelCurrentZoomScale = myUnselectedCellModel.titleLabelSelectedZoomScale;
  57. myselectedCellModel.titleLabelCurrentStrokeWidth = myUnselectedCellModel.titleLabelSelectedStrokeWidth;
  58. }
  59. - (void)refreshLeftCellModel:(JXCategoryBaseCellModel *)leftCellModel rightCellModel:(JXCategoryBaseCellModel *)rightCellModel ratio:(CGFloat)ratio {
  60. [super refreshLeftCellModel:leftCellModel rightCellModel:rightCellModel ratio:ratio];
  61. JXCategoryTitleCellModel *leftModel = (JXCategoryTitleCellModel *)leftCellModel;
  62. JXCategoryTitleCellModel *rightModel = (JXCategoryTitleCellModel *)rightCellModel;
  63. if (self.isTitleLabelZoomEnabled && self.isTitleLabelZoomScrollGradientEnabled) {
  64. leftModel.titleLabelCurrentZoomScale = [JXCategoryFactory interpolationFrom:self.titleLabelZoomScale to:1.0 percent:ratio];
  65. rightModel.titleLabelCurrentZoomScale = [JXCategoryFactory interpolationFrom:1.0 to:self.titleLabelZoomScale percent:ratio];
  66. }
  67. if (self.isTitleLabelStrokeWidthEnabled) {
  68. leftModel.titleLabelCurrentStrokeWidth = [JXCategoryFactory interpolationFrom:leftModel.titleLabelSelectedStrokeWidth to:leftModel.titleLabelNormalStrokeWidth percent:ratio];
  69. rightModel.titleLabelCurrentStrokeWidth = [JXCategoryFactory interpolationFrom:rightModel.titleLabelNormalStrokeWidth to:rightModel.titleLabelSelectedStrokeWidth percent:ratio];
  70. }
  71. if (self.isTitleColorGradientEnabled) {
  72. leftModel.titleCurrentColor = [JXCategoryFactory interpolationColorFrom:self.titleSelectedColor to:self.titleColor percent:ratio];
  73. rightModel.titleCurrentColor = [JXCategoryFactory interpolationColorFrom:self.titleColor to:self.titleSelectedColor percent:ratio];
  74. }
  75. }
  76. - (CGFloat)preferredCellWidthAtIndex:(NSInteger)index {
  77. if (self.cellWidth == JXCategoryViewAutomaticDimension) {
  78. if (self.titleDataSource && [self.titleDataSource respondsToSelector:@selector(categoryTitleView:widthForTitle:)]) {
  79. return [self.titleDataSource categoryTitleView:self widthForTitle:self.titles[index]];
  80. }else {
  81. return ceilf([self.titles[index] boundingRectWithSize:CGSizeMake(MAXFLOAT, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.titleFont} context:nil].size.width);
  82. }
  83. }else {
  84. return self.cellWidth;
  85. }
  86. }
  87. - (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index {
  88. [super refreshCellModel:cellModel index:index];
  89. JXCategoryTitleCellModel *model = (JXCategoryTitleCellModel *)cellModel;
  90. model.title = self.titles[index];
  91. model.titleNumberOfLines = self.titleNumberOfLines;
  92. model.titleFont = self.titleFont;
  93. model.titleSelectedFont = self.titleSelectedFont;
  94. model.titleNormalColor = self.titleColor;
  95. model.titleSelectedColor = self.titleSelectedColor;
  96. model.titleLabelMaskEnabled = self.isTitleLabelMaskEnabled;
  97. model.titleLabelZoomEnabled = self.isTitleLabelZoomEnabled;
  98. model.titleLabelNormalZoomScale = 1;
  99. model.titleLabelSelectedZoomScale = self.titleLabelZoomScale;
  100. model.titleLabelStrokeWidthEnabled = self.isTitleLabelStrokeWidthEnabled;
  101. model.titleLabelNormalStrokeWidth = 0;
  102. model.titleLabelSelectedStrokeWidth = self.titleLabelSelectedStrokeWidth;
  103. model.titleLabelVerticalOffset = self.titleLabelVerticalOffset;
  104. model.titleLabelAnchorPointStyle = self.titleLabelAnchorPointStyle;
  105. if (index == self.selectedIndex) {
  106. model.titleCurrentColor = model.titleSelectedColor;
  107. model.titleLabelCurrentZoomScale = model.titleLabelSelectedZoomScale;
  108. model.titleLabelCurrentStrokeWidth= model.titleLabelSelectedStrokeWidth;
  109. }else {
  110. model.titleCurrentColor = model.titleNormalColor;
  111. model.titleLabelCurrentZoomScale = model.titleLabelNormalZoomScale;
  112. model.titleLabelCurrentStrokeWidth = model.titleLabelNormalStrokeWidth;
  113. }
  114. }
  115. @end