123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- //
- // RQExerciseAnswerItemViewModel.m
- // JSJP
- //
- // Created by 张嵘 on 2021/8/16.
- //
- #import "RQExerciseAnswerItemViewModel.h"
- @interface RQExerciseAnswerItemViewModel ()
- @property (nonatomic, readwrite, strong) NSString *answerString;
- @property (nonatomic, readwrite, strong) RQYDTJSModel *ydtJSModel;
- @property (nonatomic, readwrite, assign) CGFloat skillHeight;
- @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
- @property (nonatomic, readwrite, strong) NSMutableAttributedString *skillString;
- @end
- @implementation RQExerciseAnswerItemViewModel
- - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
- if (self = [super init]) {
- @weakify(self)
- dispatch_async(dispatch_get_main_queue(), ^{
- @strongify(self)
- NSArray *allOptionsArr = @[@"A",@"B",@"C",@"D"];
-
- NSMutableArray *answerOptionArr = @[].mutableCopy;
- for (int i = 0; i < exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) {
- NSRange range;
- range.location = i;
- range.length = 1;
- NSString *indexStr = [exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range];
- [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]];
- }
- self.ydtJSModel = [RQ_YDT_JS_Question_Module getQuestionJSWithQuestionId:exerciseModel.ydtQuestionModel.ID];
- // //ning 2023-5-24 一点通db 解题答案数据有异常-新整jq库进行修正。
- // self.ydtJSModel.qb_read_analyse = [YN_YDT_JQ_Question_Module getQuestionKTJQWithQuestionId:exerciseModel.ydtQuestionModel.ID];
- //于2023-08-18 年更新db完成-解决此问题
- self.answerString = [NSString stringWithFormat:@"答案 %@",[answerOptionArr.mutableCopy componentsJoinedByString:@" "]];
- __block CGFloat space = 0.f;
- if (RQObjectIsNil(self.ydtJSModel)) {
- self.skillHeight = 0.f;
- space = 0.f;
- } else {
- NSArray *knameList = [self getKeyRangeList:self.ydtJSModel.qb_analyse readText:self.ydtJSModel.qb_read_analyse];//获取关键字
- NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",self.ydtJSModel.qb_read_analyse]];
- text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
- text.yy_lineSpacing = 8;
- text.yy_color = RQ_MAIN_TEXT_COLOR_1;
- if(knameList){//替换关键字标红
- @try {
- for (NSDictionary *item in knameList) {
- NSArray *values = item.allValues.firstObject;
- NSString *replacementString = item.allKeys.firstObject;
- NSRange range = NSMakeRange([values[0] intValue], [values[1] intValue]);
- NSDictionary *attributes = @{ NSForegroundColorAttributeName: RGB_COLOR(244, 0, 47),NSFontAttributeName: [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]};
- [text replaceCharactersInRange:range withString:replacementString];
- [text setAttributes:attributes range:NSMakeRange(range.location, range.length)];
- }
- } @catch (NSException *exception) {
- NSLog(@"替换替换发生错误");
- }
- }
- self.skillString = text;
- 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));
- CGFloat height = (line == 0)? [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel] : RQ_FIT_HORIZONTAL(34.f);
- self.skillHeight = height;
- space = 6.f;
- }
- self.rowHeight = [_answerString heightForFont:[UIFont systemFontOfSize:19] width:RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2)] + (8 * 2) + (16 * 2) + space + self.skillHeight;
- });
-
-
- }
- return self;
- }
- //获取字典
- - (NSArray *)getKeyRangeList:(NSString *)originalText readText:(NSString*)readText {
- NSError *error = nil;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"【(.*?)】" options:0 error:&error];
- NSMutableArray *array = nil;
- if (!error) {
- array = @[].mutableCopy;
- NSArray<NSTextCheckingResult *> *matches = [regex matchesInString:originalText options:0 range:NSMakeRange(0, originalText.length)];
- for (NSTextCheckingResult *match in matches) {
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- NSRange matchedRange = [match rangeAtIndex:1];
- NSString *extractedText = [originalText substringWithRange:matchedRange];
- NSRange range = [readText rangeOfString:extractedText];
- dict[extractedText] = @[@(range.location),@(range.length)];
- [array addObject:dict];
- NSLog(@"Extracted Text: %@", extractedText);
- NSLog(@"Matched Range: %@", NSStringFromRange(matchedRange));
- }
- } else {
- NSLog(@"Regex Error: %@", error.localizedDescription);
- }
- return array;
- }
- - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
- lb.attributedText = attributedStr;
- CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
- YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
- lb.textLayout = layout;
- CGFloat introHeight = layout.textBoundingSize.height+10;
- return introHeight;
- }
- - (YYLabel *)yyLabel {
- if (!_yyLabel) {
- _yyLabel = [[YYLabel alloc] init];
- }
- return _yyLabel;
- }
- - (NSString *)itemClassName {
- return @"RQExerciseAnswerCell";
- }
- @end
|