|
| 1 | +// |
| 2 | +// MathSubjectView.m |
| 3 | +// test |
| 4 | +// |
| 5 | +// Created by zkhz on 2016/12/1. |
| 6 | +// Copyright © 2016年 zkhz. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "MathSubjectView.h" |
| 10 | +#import "MathView.h" |
| 11 | +#import "YYText.h" |
| 12 | +#import "UIView+YYAdd.h" |
| 13 | +#import "UIImageView+WebCache.h" |
| 14 | +#import "MathModel.h" |
| 15 | +@interface MathSubjectView () |
| 16 | +@property (nonatomic,strong)YYLabel *label; |
| 17 | +@property (nonatomic,assign)NSUInteger renderCount; |
| 18 | +@end |
| 19 | +@implementation MathSubjectView |
| 20 | + |
| 21 | +/** |
| 22 | + lazy loading |
| 23 | +
|
| 24 | + @return instance |
| 25 | + */ |
| 26 | +- (YYLabel *)label{ |
| 27 | + if (!_label) { |
| 28 | + _label = [YYLabel new]; |
| 29 | + _label.userInteractionEnabled = YES; |
| 30 | + _label.numberOfLines = 0; |
| 31 | + _label.textVerticalAlignment = YYTextVerticalAlignmentCenter; |
| 32 | + _label.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); |
| 33 | + } |
| 34 | + return _label; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + override |
| 39 | +
|
| 40 | + @param frame frame |
| 41 | + @return instance |
| 42 | + */ |
| 43 | +- (instancetype)initWithFrame:(CGRect)frame{ |
| 44 | + if (self = [super initWithFrame:frame]) { |
| 45 | + CGRect rect_s = self.frame; |
| 46 | + rect_s.size.height = 0; |
| 47 | + self.frame = rect_s; |
| 48 | + [self addSubview:self.label]; |
| 49 | + self.renderCount = 0; |
| 50 | + } |
| 51 | + return self; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + 处理数据源并进行渲染 |
| 56 | +
|
| 57 | + @param arr 数据源 |
| 58 | + */ |
| 59 | +- (void)typesetMathWithArray:(NSArray *)arr{ |
| 60 | + NSMutableArray *arrNew = [NSMutableArray arrayWithArray:arr]; |
| 61 | + for (NSInteger i = 0; i < arr.count; i ++) { |
| 62 | + NSString *str = arr[i]; |
| 63 | + if ([str hasPrefix:@"math>"]) { |
| 64 | + NSArray *arr_t = [str componentsSeparatedByString:@">"]; |
| 65 | + __block MathView *math = [[MathView alloc]initWithRenderCompleted:^(CGRect frame,NSInteger index) { |
| 66 | + MathModel *model = [[MathModel alloc]init]; |
| 67 | + model.mathView = math; |
| 68 | + model.rect = frame; |
| 69 | + [arrNew replaceObjectAtIndex:index withObject:model]; |
| 70 | + self.renderCount ++; |
| 71 | + [self typesetWithOriArr:arr andMultiArr:arrNew]; |
| 72 | + } withCode:arr_t[1] Index:i]; |
| 73 | + math.ratio = 0.6; |
| 74 | + [self addSubview:math]; |
| 75 | + }else if([str hasPrefix:@"image>"]){ |
| 76 | + NSArray *arr_m = [str componentsSeparatedByString:@">"]; |
| 77 | + [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:arr_m[1]] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) { |
| 78 | + } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { |
| 79 | + UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; |
| 80 | + imageView.frame = CGRectMake(0, 0, image.size.width/3, image.size.height/3); |
| 81 | + NSUInteger imageIndex = [arr indexOfObject:[NSString stringWithFormat:@"image>%@",imageURL.absoluteString]]; |
| 82 | + [arrNew replaceObjectAtIndex:imageIndex withObject:imageView]; |
| 83 | + self.renderCount ++; |
| 84 | + [self typesetWithOriArr:arr andMultiArr:arrNew]; |
| 85 | + }]; |
| 86 | + }else{ |
| 87 | + [arrNew replaceObjectAtIndex:i withObject:str]; |
| 88 | + self.renderCount ++; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + 解析固定协议下的数据字符串为数组 |
| 95 | +
|
| 96 | + @param str 符合协议的数据字符串 |
| 97 | + @return 数据源数组 |
| 98 | + */ |
| 99 | +- (NSArray *)parserString:(NSString *)str{ |
| 100 | + NSArray *arr = [str componentsSeparatedByString:@"<"]; |
| 101 | + NSLog(@"%@",arr); |
| 102 | + NSMutableArray *newArr = [NSMutableArray arrayWithArray:arr]; |
| 103 | + for (NSInteger i = 0; i < newArr.count; i ++) { |
| 104 | + if ([newArr[i] hasPrefix:@"/math>"]||[newArr[i] hasPrefix:@"/image>"]) { |
| 105 | + NSString *newStr = [newArr[i] stringByReplacingOccurrencesOfString:@"/math>" withString:@""]; |
| 106 | + NSString *newStr1 = [newStr stringByReplacingOccurrencesOfString:@"/image>" withString:@""]; |
| 107 | + [newArr replaceObjectAtIndex:i withObject:newStr1]; |
| 108 | + } |
| 109 | + } |
| 110 | + NSLog(@"%@",newArr); |
| 111 | + return newArr; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + 渲染操作 |
| 116 | +
|
| 117 | + @param arr 需要渲染的元数组 |
| 118 | + @param arrNew 经过数据处理的可变元数组(渲染对象) |
| 119 | + */ |
| 120 | +- (void)typesetWithOriArr:(NSArray *)arr andMultiArr:(NSMutableArray *)arrNew{ |
| 121 | + if (self.renderCount == arr.count) { |
| 122 | + NSMutableAttributedString *text = [NSMutableAttributedString new]; |
| 123 | + |
| 124 | + for (id obj in arrNew) { |
| 125 | + if ([obj isKindOfClass:[NSString class]]) { |
| 126 | + NSString *atStr = (NSString *)obj; |
| 127 | + [text appendAttributedString:[[NSAttributedString alloc] initWithString:atStr]]; |
| 128 | + }else if ([obj isKindOfClass:[MathModel class]]){ |
| 129 | + MathModel *model = (MathModel *)obj; |
| 130 | + MathView *mathView = model.mathView; |
| 131 | + CGRect rect = model.rect; |
| 132 | + [mathView sizeToFit]; |
| 133 | + NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:mathView contentMode:0 attachmentSize:rect.size alignToFont:_font alignment:1]; |
| 134 | + [text appendAttributedString:attachText]; |
| 135 | + }else if ([obj isKindOfClass:[UIImageView class]]){ |
| 136 | + UIImageView *imageView = (UIImageView *)obj; |
| 137 | + NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:0 attachmentSize:imageView.size alignToFont:_font alignment:1]; |
| 138 | + [text appendAttributedString:attachText]; |
| 139 | + } |
| 140 | + } |
| 141 | + self.label.attributedText = text; |
| 142 | + CGSize size = CGSizeMake(self.frame.size.width, CGFLOAT_MAX); |
| 143 | + text.yy_font = _font; |
| 144 | + YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text]; |
| 145 | + self.label.size = layout.textBoundingSize; |
| 146 | + self.label.textLayout = layout; |
| 147 | + CGRect rect_s = self.frame; |
| 148 | + rect_s.size.height = self.label.height; |
| 149 | + self.frame = rect_s; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + 数据源字符串setter |
| 155 | +
|
| 156 | + @param testStr 数据源字符串 |
| 157 | + */ |
| 158 | +- (void)setTestStr:(NSString *)testStr{ |
| 159 | + _testStr = testStr; |
| 160 | + NSArray *arr = [self parserString:self.testStr]; |
| 161 | + [self typesetMathWithArray:arr]; |
| 162 | +} |
| 163 | +@end |
0 commit comments