JXCategorySubTitleCell.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // JXCategorySubTitleCell.m
  3. // ObjcExample
  4. //
  5. // Created by gaokun on 2021/1/21.
  6. //
  7. #import "JXCategorySubTitleCell.h"
  8. #import "JXCategorySubTitleCellModel.h"
  9. @implementation JXCategorySubTitleCell
  10. - (void)initializeViews {
  11. [super initializeViews];
  12. _subTitleLabel = [[UILabel alloc] init];
  13. self.subTitleLabel.clipsToBounds = YES;
  14. self.subTitleLabel.textAlignment = NSTextAlignmentCenter;
  15. self.subTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  16. [self.contentView addSubview:self.subTitleLabel];
  17. }
  18. - (void)layoutSubviews {
  19. [super layoutSubviews];
  20. JXCategorySubTitleCellModel *myCellModel = (JXCategorySubTitleCellModel *)self.cellModel;
  21. CGFloat positionMargin = myCellModel.subTitleWithTitlePositionMargin;
  22. [NSLayoutConstraint deactivateConstraints:self.subTitleLabel.constraints];
  23. switch (myCellModel.positionStyle) {
  24. case JXCategorySubTitlePositionStyle_Top: {
  25. [self.subTitleLabel.bottomAnchor constraintEqualToAnchor:self.titleLabel.topAnchor constant:positionMargin].active = YES;
  26. [self subTitleLeftRightConstraint:myCellModel];
  27. break;
  28. }
  29. case JXCategorySubTitlePositionStyle_Left: {
  30. self.titleLabelCenterX.active = NO;
  31. [self.titleLabel.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:0].active = YES;
  32. [self.subTitleLabel.rightAnchor constraintEqualToAnchor:self.titleLabel.leftAnchor constant:positionMargin].active = YES;
  33. [self subTitleTopBottomConstraint:myCellModel];
  34. break;
  35. }
  36. case JXCategorySubTitlePositionStyle_Bottom: {
  37. [self.subTitleLabel.topAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:positionMargin].active = YES;
  38. [self subTitleLeftRightConstraint:myCellModel];
  39. break;
  40. }
  41. case JXCategorySubTitlePositionStyle_Right: {
  42. self.titleLabelCenterX.active = NO;
  43. [self.titleLabel.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:0].active = YES;
  44. [self.subTitleLabel.leftAnchor constraintEqualToAnchor:self.titleLabel.rightAnchor constant:positionMargin].active = YES;
  45. [self subTitleTopBottomConstraint:myCellModel];
  46. break;
  47. }
  48. }
  49. [self.subTitleLabel.widthAnchor constraintEqualToConstant:ceilf(myCellModel.subTitleSize.width)].active = YES;
  50. [self.subTitleLabel.heightAnchor constraintEqualToConstant:ceilf(myCellModel.subTitleSize.height)].active = YES;
  51. }
  52. - (void)reloadData:(JXCategoryBaseCellModel *)cellModel {
  53. [super reloadData:cellModel];
  54. JXCategorySubTitleCellModel *myCellModel = (JXCategorySubTitleCellModel *)cellModel;
  55. if (myCellModel.isSelected) {
  56. self.subTitleLabel.font = myCellModel.subTitleSelectedFont;
  57. }else {
  58. self.subTitleLabel.font = myCellModel.subTitleFont;
  59. }
  60. NSString *titleString = myCellModel.subTitle ? myCellModel.subTitle : @"";
  61. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString];
  62. self.subTitleLabel.attributedText = attributedString;
  63. self.subTitleLabel.textColor = myCellModel.subTitleCurrentColor;
  64. }
  65. - (void)subTitleLeftRightConstraint:(JXCategorySubTitleCellModel *)cellModel {
  66. CGFloat alignMargin = cellModel.subTitleWithTitleAlignMargin;
  67. switch (cellModel.alignStyle) {
  68. case JXCategorySubTitleAlignStyle_Left: {
  69. [self.subTitleLabel.leftAnchor constraintEqualToAnchor:self.titleLabel.leftAnchor constant:alignMargin].active = YES;
  70. break;
  71. }
  72. case JXCategorySubTitleAlignStyle_Right: {
  73. [self.subTitleLabel.rightAnchor constraintEqualToAnchor:self.titleLabel.rightAnchor constant:alignMargin].active = YES;
  74. break;
  75. }
  76. default:
  77. self.subTitleLabelCenterX = [self.subTitleLabel.centerXAnchor constraintEqualToAnchor:self.titleLabel.centerXAnchor constant:alignMargin];
  78. self.subTitleLabelCenterX.active = YES;
  79. break;
  80. }
  81. }
  82. - (void)subTitleTopBottomConstraint:(JXCategorySubTitleCellModel *)cellModel {
  83. CGFloat alignMargin = cellModel.subTitleWithTitleAlignMargin;
  84. switch (cellModel.alignStyle) {
  85. case JXCategorySubTitleAlignStyle_Top: {
  86. [self.subTitleLabel.topAnchor constraintEqualToAnchor:self.titleLabel.topAnchor constant:alignMargin].active = YES;
  87. break;
  88. }
  89. case JXCategorySubTitleAlignStyle_Bottom: {
  90. [self.subTitleLabel.bottomAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor constant:alignMargin].active = YES;
  91. break;
  92. }
  93. default:
  94. [self.subTitleLabel.centerYAnchor constraintEqualToAnchor:self.titleLabel.centerYAnchor constant:alignMargin].active = YES;
  95. break;
  96. }
  97. }
  98. @end