Skip to content

Commit 0655220

Browse files
authored
Main files
1 parent 3e8c794 commit 0655220

9 files changed

+434
-0
lines changed

MathModel.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// MathModel.h
3+
// test
4+
//
5+
// Created by zkhz on 2016/12/1.
6+
// Copyright © 2016年 zkhz. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "MathView.h"
11+
@interface MathModel : NSObject
12+
13+
/**
14+
mathView
15+
*/
16+
@property (nonatomic,strong)MathView *mathView;
17+
18+
/**
19+
mathview's frame
20+
*/
21+
@property (nonatomic,assign)CGRect rect;
22+
@end

MathModel.m

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// MathModel.m
3+
// test
4+
//
5+
// Created by zkhz on 2016/12/1.
6+
// Copyright © 2016年 zkhz. All rights reserved.
7+
//
8+
9+
#import "MathModel.h"
10+
11+
@implementation MathModel
12+
@end

MathSubjectView.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// MathSubjectView.h
3+
// test
4+
//
5+
// Created by zkhz on 2016/12/1.
6+
// Copyright © 2016年 zkhz. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface MathSubjectView : UIView
12+
13+
/**
14+
字体
15+
*/
16+
@property (nonatomic,strong)UIFont *font;
17+
18+
/**
19+
数据源字符串
20+
*/
21+
@property (nonatomic,copy)NSString *testStr;
22+
@end

MathSubjectView.m

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

MathView.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// MathView.h
3+
// test
4+
//
5+
// Created by zkhz on 2016/11/29.
6+
// Copyright © 2016年 zkhz. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import "MathWebView.h"
11+
@interface MathView : UIView
12+
13+
/**
14+
scale
15+
*/
16+
@property (nonatomic,assign)CGFloat ratio;
17+
18+
/**
19+
init func
20+
21+
@param callBack self
22+
@param code LaTex Code String
23+
@return view instance
24+
*/
25+
- (instancetype)initWithRenderCompleted:(void(^)(CGRect frame,NSInteger index))callBack withCode:(NSString *)code Index:(NSInteger)index;
26+
@end

MathView.m

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// MathView.m
3+
// test
4+
//
5+
// Created by zkhz on 2016/11/29.
6+
// Copyright © 2016年 zkhz. All rights reserved.
7+
//
8+
9+
#import "MathView.h"
10+
@interface MathView ()<WKNavigationDelegate>
11+
@property (nonatomic,strong)MathWebView * webView;
12+
@property (nonatomic,copy)void(^callBack)(CGRect frame,NSInteger index);
13+
@property (nonatomic,assign)NSInteger index;
14+
@end
15+
@implementation MathView
16+
17+
/**
18+
init
19+
20+
@param callBack 回调,并返回当前frame以及当前位置index
21+
@param code Latex code
22+
@param index 当前位置index
23+
@return instance
24+
*/
25+
- (instancetype)initWithRenderCompleted:(void (^)(CGRect,NSInteger))callBack withCode:(NSString *)code Index:(NSInteger)index{
26+
if (self = [super init]) {
27+
self.frame = CGRectZero;
28+
self.callBack = callBack;
29+
self.index = index;
30+
_webView = [[MathWebView alloc]initWithLaTexCode:code];
31+
_webView.ratio = self.ratio;
32+
_webView.navigationDelegate = self;
33+
[self addSubview:_webView];
34+
self.clipsToBounds = 0;
35+
}
36+
return self;
37+
}
38+
#pragma marks - webviewDelegate
39+
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
40+
NSLog(@"开始加载");
41+
}
42+
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
43+
NSLog(@"加载完成");
44+
//这个方法也可以计算出webView滚动视图滚动的高度
45+
[webView evaluateJavaScript:@"document.getElementById(\"mykatex\").offsetWidth"completionHandler:^(id _Nullable result,NSError * _Nullable error){
46+
CGFloat newwidth = [result floatValue] * 1.05 * (self.ratio);
47+
[webView evaluateJavaScript:@"document.body.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){
48+
CGFloat newHeight = [result floatValue] * (self.ratio);
49+
NSLog(@"scrollHeight高度:%.2f",newHeight);
50+
NSLog(@"scrollWidth宽度:%.2f",newwidth);
51+
[self resetWebViewFrameWithHeight:newHeight];
52+
[self resetWebViewFrameWithWidth:newwidth];
53+
self.callBack(self.frame,self.index);
54+
}];
55+
}];
56+
}
57+
58+
/**
59+
重置webview,self高
60+
61+
@param newheight newheight
62+
*/
63+
- (void)resetWebViewFrameWithHeight:(CGFloat)newheight{
64+
CGRect rect = _webView.frame;
65+
CGRect rect_s = self.frame;
66+
rect.size.height = newheight;
67+
rect_s.size.height = newheight;
68+
_webView.frame = rect;
69+
self.frame = rect_s;
70+
}
71+
72+
/**
73+
重置webview,self宽
74+
75+
@param newwidth newwidth
76+
*/
77+
- (void)resetWebViewFrameWithWidth:(CGFloat)newwidth{
78+
CGRect rect = _webView.frame;
79+
CGRect rect_s = self.frame;
80+
rect.size.width = newwidth;
81+
rect_s.size.width = newwidth;
82+
_webView.frame = rect;
83+
self.frame = rect_s;
84+
}
85+
@end

MathWebView.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// MathWebView.h
3+
// test
4+
//
5+
// Created by zkhz on 2016/11/29.
6+
// Copyright © 2016年 zkhz. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <WebKit/WebKit.h>
11+
@interface MathWebView :WKWebView
12+
13+
/**
14+
scale
15+
*/
16+
@property (nonatomic,assign)CGFloat ratio;
17+
18+
/**
19+
init func with Latex code
20+
21+
@param code Latex code
22+
@return instance
23+
*/
24+
- (instancetype)initWithLaTexCode:(NSString *)code;
25+
@end

0 commit comments

Comments
 (0)