RQExerciseOptionCell.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // RQExerciseOptionCell.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/12.
  6. //
  7. #import "RQExerciseOptionCell.h"
  8. @interface RQExerciseOptionCell ()
  9. @property (nonatomic, readwrite, strong) RQExerciseOptionItemViewModel *viewModel;
  10. @property (weak, nonatomic) IBOutlet QMUIButton *optionBtn;
  11. @property (weak, nonatomic) IBOutlet UILabel *optionLabel;
  12. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *optionBtnWidth;
  13. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelHeight;
  14. @end
  15. @implementation RQExerciseOptionCell
  16. #pragma mark - PublicMethods
  17. + (instancetype)cellWithTableView:(UITableView *)tableView {
  18. static NSString *ID = @"RQExerciseOptionCell";
  19. RQExerciseOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  20. if (!cell) {
  21. cell = [self rq_viewFromXib];
  22. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  23. cell.optionBtn.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.16].CGColor;
  24. }
  25. return cell;
  26. }
  27. - (void)bindViewModel:(RQExerciseOptionItemViewModel *)viewModel {
  28. @weakify(self)
  29. _viewModel = viewModel;
  30. RAC(self.optionLabel, attributedText) = [[RACObserve(viewModel, optsString) takeUntil:self.rac_prepareForReuseSignal] deliverOnMainThread];
  31. RAC(self.labelHeight, constant) = [[RACObserve(viewModel, labelHeight) takeUntil:self.rac_prepareForReuseSignal] deliverOnMainThread];
  32. RAC(self, optionBtn) = [[[RACSignal combineLatest:@[[RACObserve(RQ_Exercise_Module, exerciseFontSize) takeUntil:self.rac_prepareForReuseSignal],[RACObserve(viewModel, btnBgColor) takeUntil:self.rac_prepareForReuseSignal]] reduce:^(NSNumber *exerciseFontSize, UIColor *btnBgColor) {
  33. @strongify(self)
  34. QMUIButton *myOptionBtn = self.optionBtn;
  35. [myOptionBtn setTitleNormal:viewModel.title];
  36. myOptionBtn.titleLabel.font = [UIFont systemFontOfSize:exerciseFontSize.integerValue];
  37. CGFloat btnWidth = RQ_FIT_HORIZONTAL(30.f) + (exerciseFontSize.integerValue - 17);
  38. self.optionBtnWidth.constant = btnWidth;
  39. myOptionBtn.layer.cornerRadius = btnWidth / 2.f;
  40. myOptionBtn.layer.masksToBounds = NO;
  41. myOptionBtn.layer.shadowOffset = CGSizeMake(0, 2);
  42. myOptionBtn.layer.shadowRadius = 6;
  43. myOptionBtn.layer.opacity = 1;
  44. myOptionBtn.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.16].CGColor;
  45. myOptionBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
  46. if (RQStringIsEmpty(viewModel.title)) {
  47. if ([btnBgColor isEqual:RQ_MAIN_TEXT_COLOR_RED]) {
  48. [myOptionBtn setImage:RQImageNamed(@"ErrorOption") forState:UIControlStateNormal];
  49. } else if ([btnBgColor isEqual:RQ_MAIN_TEXT_COLOR_GREEN]) {
  50. [myOptionBtn setImage:RQImageNamed(@"CorrectOption") forState:UIControlStateNormal];
  51. }
  52. }
  53. UIImage *btnBackgroundImage = [UIImage qmui_imageWithColor:btnBgColor size:CGSizeMake(btnWidth, btnWidth) cornerRadius:btnWidth / 2.f];
  54. [myOptionBtn setBackgroundImage:btnBackgroundImage forState:UIControlStateNormal];
  55. if ([btnBgColor isEqual:RQ_MAIN_TEXT_COLOR_WHITE]) {
  56. [myOptionBtn setTitleColor:RQ_MAIN_TEXT_COLOR_1 forState:UIControlStateNormal];
  57. } else {
  58. [myOptionBtn setTitleColor:RQ_MAIN_TEXT_COLOR_WHITE forState:UIControlStateNormal];
  59. }
  60. return myOptionBtn;
  61. }] deliverOnMainThread] takeUntil:self.rac_prepareForReuseSignal];
  62. }
  63. #pragma mark - SystemMethods
  64. - (void)awakeFromNib {
  65. [super awakeFromNib];
  66. }
  67. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  68. [super setSelected:selected animated:animated];
  69. }
  70. @end