// // RQExerciseAnswerItemViewModel.m // JSJP // // Created by 张嵘 on 2021/8/16. // #import "RQExerciseAnswerItemViewModel.h" @interface RQExerciseAnswerItemViewModel (){ CGFloat _exRowHeight;//记录原始rowHeight } @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; @property (nonatomic, readwrite, strong) QMUIButton *readBtn; @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]; exerciseModel.ydtQuestionModel.explain = self.ydtJSModel.best_explain_new;//2023.9.25 // //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 { NSString *readText = [[self.ydtJSModel.qb_analyse stringByReplacingOccurrencesOfString:@"【" withString:@""] stringByReplacingOccurrencesOfString:@"】" withString:@""]; if(readText==nil&&exerciseModel.ydtQuestionModel.Type==RQQuestionType_Judgment){//判断题 readText = [NSString stringWithFormat:@"看题目答题:%@",[answerOptionArr.mutableCopy componentsJoinedByString:@" "]]; } NSArray *knameList = [self getKeyRangeList:self.ydtJSModel.qb_analyse readText:readText];//获取关键字 NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init]; //播放-答案 if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启 CGSize btnSize = CGSizeMake(30, 30); //读题 QMUIButton *readBtn = [QMUIButton buttonWithType:UIButtonTypeCustom]; CGFloat ebtnH = 30.f; CGFloat ebtnW = 30.f; CGSize ebtnSize = CGSizeMake(ebtnW, ebtnH); readBtn.size = btnSize; [readBtn setImage:RQImageNamed(@"播放_pay") forState:UIControlStateNormal]; [readBtn setImage:RQImageNamed(@"播放_suspend") forState:UIControlStateSelected]; [readBtn addTarget:self action:@selector(buttonActionPayVoicedo:) forControlEvents:UIControlEventTouchUpInside]; NSMutableAttributedString *readAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:readBtn contentMode:UIViewContentModeCenter attachmentSize:ebtnSize alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter]; self.readBtn = readBtn; [text appendAttributedString:readAttachment]; } NSMutableAttributedString *analyseString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",readText]]; [text appendAttributedString:analyseString]; 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]+1, [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)); if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看 line = 0; } CGFloat height = (line == 0)? [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel] : RQ_FIT_HORIZONTAL(34.f); self.skillHeight = height; space = 6.f; } _exRowHeight = [_answerString heightForFont:[UIFont systemFontOfSize:19] width:RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2)] + (8 * 2) + (16 * 2) + space; }); } return self; } //重写 skillHeight get 方法 - (CGFloat)skillHeight{ CGFloat skillH = _skillHeight; 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)); if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看 line = 0; } if(line==1) skillH = RQ_FIT_HORIZONTAL(34.f); return skillH; } //重写 rowHeight get 方法 - (CGFloat)rowHeight{ CGFloat rowH = _exRowHeight + self.skillHeight; 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)); if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看 rowH = rowH + 40.f; } return rowH; } - (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]; _yyLabel.userInteractionEnabled = YES; } return _yyLabel; } - (NSString *)itemClassName { return @"RQExerciseAnswerCell"; } #pragma mark -事件 - (void)buttonActionPayVoicedo:(UIButton *)btn { if (RQ_COMMON_MANAGER.JSJP_APP_VOICE) {//读题-是否开启 //是vip 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)); if (RQ_USER_MANAGER.tryLookCount>0 && line==1){//试看 line = 0; } if (line==1) { ShowMsg(@"请购买会员,才能读解题技巧。"); return; } } btn.userInteractionEnabled = NO; btn.selected = !btn.isSelected; if(btn.selected){//播放 if (NY_VOICE_MANAGER.state == NYVoiceType_Pause){ [NY_VOICE_MANAGER resumePayVoiceActionUrl];//恢复 }else { [NY_VOICE_MANAGER stopPayVoiceActionUrl]; NSString *url = self.ydtJSModel.qb_skill_audio_link; WeakSelf(weakSelf) [NY_VOICE_MANAGER startPayVoiceActionUrl:url completedBlock:^(NYVoiceType voiceType) { switch (voiceType) { case NYVoiceType_Start:{ weakSelf.readBtn.selected = YES; }break; case NYVoiceType_Wait:{//完成-等待 weakSelf.readBtn.selected = NO; }break; case NYVoiceType_Resume:{//恢复 weakSelf.readBtn.selected = YES; }break; case NYVoiceType_Pause:{//暂停 weakSelf.readBtn.selected = NO; }break; case NYVoiceType_Stop:{//停止 weakSelf.readBtn.selected = NO; }break; default: break; } }]; } }else {//暂停 [NY_VOICE_MANAGER pausePayVoiceActionUrl]; } //防止频繁点击 1 秒点一次 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.readBtn.userInteractionEnabled = YES; }); } @end