JXCategorySubTitleView.m 5.4 KB

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