BogoJXCategoryView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // JXCategorySubTitleView.m
  3. // ObjcExample
  4. //
  5. // Created by gaokun on 2021/1/21.
  6. //
  7. #import "BogoJXCategoryView.h"
  8. #import "CustomCategoryCell.h"
  9. @implementation BogoJXCategoryView
  10. - (void)initializeData {
  11. [super initializeData];
  12. _subTitleColor = [UIColor blackColor];
  13. _subTitleSelectedColor = [UIColor blackColor];
  14. _subTitleFont = [UIFont systemFontOfSize:12];
  15. _positionStyle = JXCategorySubTitlePositionStyle_Bottom;
  16. _alignStyle = JXCategorySubTitleAlignStyle_Center;
  17. _subTitleWithTitlePositionMargin = 0;
  18. _subTitleWithTitleAlignMargin = 0;
  19. }
  20. - (UIFont *)subTitleSelectedFont {
  21. if (_subTitleSelectedFont) {
  22. return _subTitleSelectedFont;
  23. }
  24. return _subTitleFont;
  25. }
  26. #pragma mark - Override
  27. - (Class)preferredCellClass {
  28. return [CustomCategoryCell class];
  29. }
  30. - (void)refreshDataSource {
  31. NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:self.titles.count];
  32. for (int i = 0; i < self.titles.count; i++) {
  33. JXCategorySubTitleCellModel *cellModel = [[JXCategorySubTitleCellModel alloc] init];
  34. [tempArray addObject:cellModel];
  35. }
  36. self.dataSource = [NSArray arrayWithArray:tempArray];
  37. }
  38. - (void)refreshSelectedCellModel:(JXCategoryBaseCellModel *)selectedCellModel unselectedCellModel:(JXCategoryBaseCellModel *)unselectedCellModel {
  39. [super refreshSelectedCellModel:selectedCellModel unselectedCellModel:unselectedCellModel];
  40. JXCategorySubTitleCellModel *myUnselectedCellModel = (JXCategorySubTitleCellModel *)unselectedCellModel;
  41. JXCategorySubTitleCellModel *myselectedCellModel = (JXCategorySubTitleCellModel *)selectedCellModel;
  42. myselectedCellModel.subTitleCurrentColor = myselectedCellModel.subTitleSelectedColor;
  43. myUnselectedCellModel.subTitleCurrentColor = myUnselectedCellModel.subTitleNormalColor;
  44. }
  45. - (void)refreshLeftCellModel:(JXCategoryBaseCellModel *)leftCellModel rightCellModel:(JXCategoryBaseCellModel *)rightCellModel ratio:(CGFloat)ratio {
  46. [super refreshLeftCellModel:leftCellModel rightCellModel:rightCellModel ratio:ratio];
  47. }
  48. - (CGFloat)preferredCellWidthAtIndex:(NSInteger)index {
  49. if (self.cellWidth == JXCategoryViewAutomaticDimension) {
  50. if (self.titleDataSource && [self.titleDataSource respondsToSelector:@selector(categoryTitleView:widthForTitle:)]) {
  51. return [self.titleDataSource categoryTitleView:self widthForTitle:self.titles[index]];
  52. } else {
  53. CGFloat titleW = ceilf([self widthWithTitle:self.titles[index] font:self.titleFont]);
  54. CGFloat subTitleW = ceilf([self widthWithTitle:self.subTitles[index] font:self.subTitleFont]);
  55. if (self.positionStyle == JXCategorySubTitlePositionStyle_Top || self.positionStyle == JXCategorySubTitlePositionStyle_Bottom) {
  56. return MAX(titleW, subTitleW);
  57. }else {
  58. return titleW + subTitleW;
  59. }
  60. }
  61. } else {
  62. return self.cellWidth;
  63. }
  64. }
  65. - (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index {
  66. [super refreshCellModel:cellModel index:index];
  67. JXCategorySubTitleCellModel *model = (JXCategorySubTitleCellModel *)cellModel;
  68. model.subTitle = self.subTitles[index];
  69. model.subTitleFont = self.subTitleFont;
  70. model.subTitleSelectedFont = self.subTitleSelectedFont;
  71. model.subTitleNormalColor = self.subTitleColor;
  72. model.subTitleSelectedColor = self.subTitleSelectedColor;
  73. model.positionStyle = self.positionStyle;
  74. model.alignStyle = self.alignStyle;
  75. model.subTitleWithTitlePositionMargin = self.subTitleWithTitlePositionMargin;
  76. model.subTitleWithTitleAlignMargin = self.subTitleWithTitleAlignMargin;
  77. if (index == self.selectedIndex) {
  78. model.subTitleCurrentColor = model.subTitleSelectedColor;
  79. }else {
  80. model.subTitleCurrentColor = model.subTitleNormalColor;
  81. }
  82. }
  83. - (CGRect)getTargetCellFrame:(NSInteger)targetIndex {
  84. CGRect frame = [super getTargetCellFrame:targetIndex];
  85. if (self.isIgnoreSubTitleWidth) {
  86. if (targetIndex >= 0 && targetIndex < self.subTitles.count) {
  87. NSString *subTitle = self.subTitles[targetIndex];
  88. CGFloat subTitleWidth = [subTitle sizeWithAttributes:@{NSFontAttributeName: self.subTitleFont}].width;
  89. frame.size.width -= subTitleWidth;
  90. if (self.positionStyle == JXCategorySubTitlePositionStyle_Left) {
  91. frame.origin.x += subTitleWidth;
  92. }
  93. }
  94. }
  95. return frame;
  96. }
  97. - (CGRect)getTargetSelectedCellFrame:(NSInteger)targetIndex selectedType:(JXCategoryCellSelectedType)selectedType {
  98. CGRect frame = [super getTargetSelectedCellFrame:targetIndex selectedType:selectedType];
  99. if (self.isIgnoreSubTitleWidth) {
  100. if (targetIndex >= 0 && targetIndex < self.subTitles.count) {
  101. NSString *subTitle = self.subTitles[targetIndex];
  102. CGFloat subTitleWidth = [subTitle sizeWithAttributes:@{NSFontAttributeName: self.subTitleFont}].width;
  103. frame.size.width -= subTitleWidth;
  104. if (self.positionStyle == JXCategorySubTitlePositionStyle_Left) {
  105. frame.origin.x += subTitleWidth;
  106. }
  107. }
  108. }
  109. return frame;
  110. }
  111. #pragma mark - Private
  112. - (CGFloat)widthWithTitle:(NSString *)title font:(UIFont *)font {
  113. return [title boundingRectWithSize:CGSizeMake(MAXFLOAT, self.bounds.size.width) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil].size.width;
  114. }
  115. @end