CustomCategoryCell.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // JXCategorySubTitleCell.m
  3. // ObjcExample
  4. //
  5. // Created by gaokun on 2021/1/21.
  6. //
  7. #import "CustomCategoryCell.h"
  8. #import "JXCategorySubTitleCellModel.h"
  9. @implementation CustomCategoryCell
  10. - (void)initializeViews {
  11. [super initializeViews];
  12. // 初始化背景视图和颜色
  13. _backgroundView1 = [[UIView alloc] init];
  14. [self.contentView insertSubview:_backgroundView1 atIndex:0];
  15. _selectedBackgroundColor = [UIColor colorWithHexString:@"#AE2CF1"];
  16. _unselectedBackgroundColor = [UIColor colorWithHexString:@"#F0F0F0"];
  17. _subTitleLabel = [[UILabel alloc] init];
  18. self.subTitleLabel.clipsToBounds = YES;
  19. self.subTitleLabel.textAlignment = NSTextAlignmentCenter;
  20. self.subTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  21. [self.contentView addSubview:self.subTitleLabel];
  22. _backgroundView1.backgroundColor = _unselectedBackgroundColor;
  23. [_backgroundView1 mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.left.equalTo(self.titleLabel).offset(-8);
  25. make.right.equalTo(self.titleLabel).offset(8);
  26. make.top.equalTo(self.titleLabel).offset(-8);
  27. make.bottom.equalTo(self.titleLabel).offset(8);
  28. }];
  29. ViewRadius(_backgroundView1, (18+16)/2);
  30. }
  31. - (void)layoutSubviews {
  32. [super layoutSubviews];
  33. self.backgroundView.frame = self.contentView.bounds;
  34. JXCategorySubTitleCellModel *myCellModel = (JXCategorySubTitleCellModel *)self.cellModel;
  35. CGFloat positionMargin = myCellModel.subTitleWithTitlePositionMargin;
  36. [NSLayoutConstraint deactivateConstraints:self.subTitleLabel.constraints];
  37. switch (myCellModel.positionStyle) {
  38. case JXCategorySubTitlePositionStyle_Top: {
  39. [self.subTitleLabel.bottomAnchor constraintEqualToAnchor:self.titleLabel.topAnchor constant:positionMargin].active = YES;
  40. [self subTitleLeftRightConstraint:myCellModel];
  41. break;
  42. }
  43. case JXCategorySubTitlePositionStyle_Left: {
  44. self.titleLabelCenterX.active = NO;
  45. [self.titleLabel.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:0].active = YES;
  46. [self.subTitleLabel.rightAnchor constraintEqualToAnchor:self.titleLabel.leftAnchor constant:positionMargin].active = YES;
  47. [self subTitleTopBottomConstraint:myCellModel];
  48. break;
  49. }
  50. case JXCategorySubTitlePositionStyle_Bottom: {
  51. [self.subTitleLabel.topAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:positionMargin].active = YES;
  52. [self subTitleLeftRightConstraint:myCellModel];
  53. break;
  54. }
  55. case JXCategorySubTitlePositionStyle_Right: {
  56. self.titleLabelCenterX.active = NO;
  57. [self.titleLabel.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:0].active = YES;
  58. [self.subTitleLabel.leftAnchor constraintEqualToAnchor:self.titleLabel.rightAnchor constant:positionMargin].active = YES;
  59. [self subTitleTopBottomConstraint:myCellModel];
  60. break;
  61. }
  62. }
  63. [self.subTitleLabel.widthAnchor constraintEqualToConstant:ceilf(myCellModel.subTitleSize.width)].active = YES;
  64. [self.subTitleLabel.heightAnchor constraintEqualToConstant:ceilf(myCellModel.subTitleSize.height)].active = YES;
  65. }
  66. - (void)reloadData:(JXCategoryBaseCellModel *)cellModel {
  67. [super reloadData:cellModel];
  68. JXCategorySubTitleCellModel *myCellModel = (JXCategorySubTitleCellModel *)cellModel;
  69. if (myCellModel.isSelected) {
  70. self.subTitleLabel.font = myCellModel.subTitleSelectedFont;
  71. }else {
  72. self.subTitleLabel.font = myCellModel.subTitleFont;
  73. }
  74. if (myCellModel.isSelected) {
  75. self.backgroundView1.backgroundColor = self.selectedBackgroundColor;
  76. } else {
  77. self.backgroundView1.backgroundColor = self.unselectedBackgroundColor;
  78. }
  79. NSString *titleString = myCellModel.subTitle ? myCellModel.subTitle : @"";
  80. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString];
  81. self.subTitleLabel.attributedText = attributedString;
  82. self.subTitleLabel.textColor = myCellModel.subTitleCurrentColor;
  83. }
  84. - (void)subTitleLeftRightConstraint:(JXCategorySubTitleCellModel *)cellModel {
  85. CGFloat alignMargin = cellModel.subTitleWithTitleAlignMargin;
  86. switch (cellModel.alignStyle) {
  87. case JXCategorySubTitleAlignStyle_Left: {
  88. [self.subTitleLabel.leftAnchor constraintEqualToAnchor:self.titleLabel.leftAnchor constant:alignMargin].active = YES;
  89. break;
  90. }
  91. case JXCategorySubTitleAlignStyle_Right: {
  92. [self.subTitleLabel.rightAnchor constraintEqualToAnchor:self.titleLabel.rightAnchor constant:alignMargin].active = YES;
  93. break;
  94. }
  95. default:
  96. self.subTitleLabelCenterX = [self.subTitleLabel.centerXAnchor constraintEqualToAnchor:self.titleLabel.centerXAnchor constant:alignMargin];
  97. self.subTitleLabelCenterX.active = YES;
  98. break;
  99. }
  100. }
  101. - (void)subTitleTopBottomConstraint:(JXCategorySubTitleCellModel *)cellModel {
  102. CGFloat alignMargin = cellModel.subTitleWithTitleAlignMargin;
  103. switch (cellModel.alignStyle) {
  104. case JXCategorySubTitleAlignStyle_Top: {
  105. [self.subTitleLabel.topAnchor constraintEqualToAnchor:self.titleLabel.topAnchor constant:alignMargin].active = YES;
  106. break;
  107. }
  108. case JXCategorySubTitleAlignStyle_Bottom: {
  109. [self.subTitleLabel.bottomAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:alignMargin].active = YES;
  110. break;
  111. }
  112. default:
  113. [self.subTitleLabel.centerYAnchor constraintEqualToAnchor:self.titleLabel.centerYAnchor constant:alignMargin].active = YES;
  114. break;
  115. }
  116. }
  117. @end