RQExerciseAnswerItemViewModel.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // RQExerciseAnswerItemViewModel.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/16.
  6. //
  7. #import "RQExerciseAnswerItemViewModel.h"
  8. @interface RQExerciseAnswerItemViewModel ()
  9. @property (nonatomic, readwrite, strong) NSString *answerString;
  10. @property (nonatomic, readwrite, strong) RQYDTJSModel *ydtJSModel;
  11. @property (nonatomic, readwrite, assign) CGFloat skillHeight;
  12. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  13. @property (nonatomic, readwrite, strong) NSMutableAttributedString *skillString;
  14. @end
  15. @implementation RQExerciseAnswerItemViewModel
  16. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  17. if (self = [super init]) {
  18. @weakify(self)
  19. dispatch_async(dispatch_get_main_queue(), ^{
  20. @strongify(self)
  21. NSArray *allOptionsArr = @[@"A",@"B",@"C",@"D"];
  22. NSMutableArray *answerOptionArr = @[].mutableCopy;
  23. for (int i = 0; i < exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) {
  24. NSRange range;
  25. range.location = i;
  26. range.length = 1;
  27. NSString *indexStr = [exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range];
  28. [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]];
  29. }
  30. self.ydtJSModel = [RQ_YDT_JS_Question_Module getQuestionJSWithQuestionId:exerciseModel.ydtQuestionModel.ID];
  31. // //ning 2023-5-24 一点通db 解题答案数据有异常-新整jq库进行修正。
  32. // self.ydtJSModel.qb_read_analyse = [YN_YDT_JQ_Question_Module getQuestionKTJQWithQuestionId:exerciseModel.ydtQuestionModel.ID];
  33. //于2023-08-18 年更新db完成-解决此问题
  34. self.answerString = [NSString stringWithFormat:@"答案 %@",[answerOptionArr.mutableCopy componentsJoinedByString:@" "]];
  35. __block CGFloat space = 0.f;
  36. if (RQObjectIsNil(self.ydtJSModel)) {
  37. self.skillHeight = 0.f;
  38. space = 0.f;
  39. } else {
  40. NSArray *knameList = [self getKeyRangeList:self.ydtJSModel.qb_analyse readText:self.ydtJSModel.qb_read_analyse];//获取关键字
  41. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",self.ydtJSModel.qb_read_analyse]];
  42. text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  43. text.yy_lineSpacing = 8;
  44. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  45. if(knameList){//替换关键字标红
  46. @try {
  47. for (NSDictionary *item in knameList) {
  48. NSArray *values = item.allValues.firstObject;
  49. NSString *replacementString = item.allKeys.firstObject;
  50. NSRange range = NSMakeRange([values[0] intValue], [values[1] intValue]);
  51. NSDictionary *attributes = @{ NSForegroundColorAttributeName: RGB_COLOR(244, 0, 47),NSFontAttributeName: [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]};
  52. [text replaceCharactersInRange:range withString:replacementString];
  53. [text setAttributes:attributes range:NSMakeRange(range.location, range.length)];
  54. }
  55. } @catch (NSException *exception) {
  56. NSLog(@"替换替换发生错误");
  57. }
  58. }
  59. self.skillString = text;
  60. NSInteger line = RQ_COMMON_MANAGER.APP_SWITCH? 0 : ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  61. CGFloat height = (line == 0)? [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel] : RQ_FIT_HORIZONTAL(34.f);
  62. self.skillHeight = height;
  63. space = 6.f;
  64. }
  65. self.rowHeight = [_answerString heightForFont:[UIFont systemFontOfSize:19] width:RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2)] + (8 * 2) + (16 * 2) + space + self.skillHeight;
  66. });
  67. }
  68. return self;
  69. }
  70. //获取字典
  71. - (NSArray *)getKeyRangeList:(NSString *)originalText readText:(NSString*)readText {
  72. NSError *error = nil;
  73. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"【(.*?)】" options:0 error:&error];
  74. NSMutableArray *array = nil;
  75. if (!error) {
  76. array = @[].mutableCopy;
  77. NSArray<NSTextCheckingResult *> *matches = [regex matchesInString:originalText options:0 range:NSMakeRange(0, originalText.length)];
  78. for (NSTextCheckingResult *match in matches) {
  79. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  80. NSRange matchedRange = [match rangeAtIndex:1];
  81. NSString *extractedText = [originalText substringWithRange:matchedRange];
  82. NSRange range = [readText rangeOfString:extractedText];
  83. dict[extractedText] = @[@(range.location),@(range.length)];
  84. [array addObject:dict];
  85. NSLog(@"Extracted Text: %@", extractedText);
  86. NSLog(@"Matched Range: %@", NSStringFromRange(matchedRange));
  87. }
  88. } else {
  89. NSLog(@"Regex Error: %@", error.localizedDescription);
  90. }
  91. return array;
  92. }
  93. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  94. lb.attributedText = attributedStr;
  95. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  96. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  97. lb.textLayout = layout;
  98. CGFloat introHeight = layout.textBoundingSize.height+10;
  99. return introHeight;
  100. }
  101. - (YYLabel *)yyLabel {
  102. if (!_yyLabel) {
  103. _yyLabel = [[YYLabel alloc] init];
  104. }
  105. return _yyLabel;
  106. }
  107. - (NSString *)itemClassName {
  108. return @"RQExerciseAnswerCell";
  109. }
  110. @end