123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //
- // RQExercisExplainItemViewModel.m
- // JSJP
- //
- // Created by 张嵘 on 2022/7/3.
- //
- #import "RQExercisExplainItemViewModel.h"
- @interface RQExercisExplainItemViewModel ()
- @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
- @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
- @property (nonatomic, readwrite, strong) NSMutableAttributedString *explainString;
- @end
- @implementation RQExercisExplainItemViewModel
- - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
- if (self = [super init]) {
- @weakify(self)
- dispatch_async(dispatch_get_main_queue(), ^{
- @strongify(self)
- self.exerciseModel = exerciseModel;
- CGFloat headerHeight = 16.f + 26.f + 16.f;
- CGFloat textHeight = 0;
- CGFloat footerHeight = 16.f + 18.f + 16.f;
- if (!RQObjectIsNil(exerciseModel.ydtQuestionModel)) {
- if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.explain)) {
- NSString *readText = [[exerciseModel.ydtQuestionModel.explain stringByReplacingOccurrencesOfString:@"【" withString:@""] stringByReplacingOccurrencesOfString:@"】" withString:@""];
- NSArray *knameList = [self getKeyRangeList:exerciseModel.ydtQuestionModel.explain readText:readText];//获取关键字
- NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:readText];
- 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], [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.explainString = text;
- CGFloat height = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel];
- textHeight = height;
- }
- }
-
- self.rowHeight = headerHeight + textHeight + footerHeight;
- });
-
-
-
- }
- return self;
- }
- - (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;
- return introHeight;
- }
- - (YYLabel *)yyLabel {
- if (!_yyLabel) {
- _yyLabel = [[YYLabel alloc] init];
- }
- return _yyLabel;
- }
- - (NSString *)itemClassName {
- return @"RQExercisExplainCell";
- }
- @end
|