RQExercisExplainItemViewModel.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // RQExercisExplainItemViewModel.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2022/7/3.
  6. //
  7. #import "RQExercisExplainItemViewModel.h"
  8. @interface RQExercisExplainItemViewModel ()
  9. @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
  10. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  11. @property (nonatomic, readwrite, strong) NSMutableAttributedString *explainString;
  12. @end
  13. @implementation RQExercisExplainItemViewModel
  14. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  15. if (self = [super init]) {
  16. @weakify(self)
  17. dispatch_async(dispatch_get_main_queue(), ^{
  18. @strongify(self)
  19. self.exerciseModel = exerciseModel;
  20. CGFloat headerHeight = 16.f + 26.f + 16.f;
  21. CGFloat textHeight = 0;
  22. CGFloat footerHeight = 16.f + 18.f + 16.f;
  23. if (!RQObjectIsNil(exerciseModel.ydtQuestionModel)) {
  24. if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.explain)) {
  25. NSString *readText = [[exerciseModel.ydtQuestionModel.explain stringByReplacingOccurrencesOfString:@"【" withString:@""] stringByReplacingOccurrencesOfString:@"】" withString:@""];
  26. NSArray *knameList = [self getKeyRangeList:exerciseModel.ydtQuestionModel.explain readText:readText];//获取关键字
  27. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:readText];
  28. NSMutableAttributedString *analyseString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",readText]];
  29. [text appendAttributedString:analyseString];
  30. text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  31. text.yy_lineSpacing = 8;
  32. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  33. if(knameList){//替换关键字标红
  34. @try {
  35. for (NSDictionary *item in knameList) {
  36. NSArray *values = item.allValues.firstObject;
  37. NSString *replacementString = item.allKeys.firstObject;
  38. NSRange range = NSMakeRange([values[0] intValue], [values[1] intValue]);
  39. NSDictionary *attributes = @{ NSForegroundColorAttributeName: RGB_COLOR(244, 0, 47),NSFontAttributeName: [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]};
  40. [text replaceCharactersInRange:range withString:replacementString];
  41. [text setAttributes:attributes range:NSMakeRange(range.location, range.length)];
  42. }
  43. } @catch (NSException *exception) {
  44. NSLog(@"替换替换发生错误");
  45. }
  46. }
  47. self.explainString = text;
  48. CGFloat height = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel];
  49. textHeight = height;
  50. }
  51. }
  52. self.rowHeight = headerHeight + textHeight + footerHeight;
  53. });
  54. }
  55. return self;
  56. }
  57. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  58. lb.attributedText = attributedStr;
  59. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  60. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  61. lb.textLayout = layout;
  62. CGFloat introHeight = layout.textBoundingSize.height;
  63. return introHeight;
  64. }
  65. - (YYLabel *)yyLabel {
  66. if (!_yyLabel) {
  67. _yyLabel = [[YYLabel alloc] init];
  68. }
  69. return _yyLabel;
  70. }
  71. - (NSString *)itemClassName {
  72. return @"RQExercisExplainCell";
  73. }
  74. @end