Skip to content

Commit 0ee5b15

Browse files
authored
Support MathML Code
Support MathML Code
1 parent a7c9ba8 commit 0ee5b15

9 files changed

+556
-0
lines changed

MathModel.h

Lines changed: 22 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#import "MathView.h"
11+
12+
@interface MathSubjectView : UIView
13+
@property (nonatomic,assign)MathRenderEngineType type;
14+
/**
15+
字体
16+
*/
17+
@property (nonatomic,strong)UIFont *font;
18+
19+
/**
20+
数据源字符串
21+
*/
22+
@property (nonatomic,copy)NSString *testStr;
23+
- (instancetype)initWithFrame:(CGRect)frame
24+
withIndexPath:(NSIndexPath *)indexPath
25+
MathRenderEngineType:(MathRenderEngineType)type
26+
callBack:(void(^)(CGFloat height,NSIndexPath *IndexPath))callBack;
27+
@end

MathSubjectView.m

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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 "YYText.h"
11+
#import "UIView+YYAdd.h"
12+
#import "MathModel.h"
13+
@interface MathSubjectView ()
14+
@property (nonatomic,strong)YYLabel *label;
15+
@property (nonatomic,assign)NSUInteger renderCount;
16+
@property (nonatomic,strong)NSIndexPath *indexPath;
17+
@property (nonatomic,copy)void(^callBack)(CGFloat height,NSIndexPath *indexPath);
18+
@end
19+
@implementation MathSubjectView
20+
21+
/**
22+
lazy loading
23+
24+
@return instance
25+
*/
26+
- (YYLabel *)label{
27+
if (!_label) {
28+
WS(weakSelf);
29+
_label = [[YYLabel alloc]init];
30+
_label.numberOfLines = 0;
31+
_label.backgroundColor = [UIColor whiteColor];
32+
_label.displaysAsynchronously = YES;
33+
_label.ignoreCommonProperties = YES;
34+
_label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
35+
_label.frame = CGRectMake(0, 0, weakSelf.frame.size.width, weakSelf.frame.size.height);
36+
}
37+
return _label;
38+
}
39+
40+
/**
41+
override
42+
43+
@param frame frame
44+
@return instance
45+
*/
46+
- (instancetype)initWithFrame:(CGRect)frame withIndexPath:(NSIndexPath *)indexPath MathRenderEngineType:(MathRenderEngineType)type callBack:(void (^)(CGFloat, NSIndexPath *))callBack{
47+
if (self = [super initWithFrame:frame]) {
48+
self.type = type;
49+
self.indexPath = indexPath;
50+
self.callBack = callBack;
51+
[self addSubview:self.label];
52+
self.renderCount = 0;
53+
}
54+
return self;
55+
}
56+
57+
/**
58+
处理数据源并进行渲染
59+
60+
@param arr 数据源
61+
*/
62+
- (void)typesetMathWithArray:(NSArray *)arr{
63+
WS(weakSelf);
64+
NSMutableArray *arrNew = [NSMutableArray arrayWithArray:arr];
65+
for (NSInteger i = 0; i < arr.count; i ++) {
66+
NSString *str = arr[i];
67+
if ([str hasPrefix:@"math#"]) {
68+
NSArray *arr_t = [str componentsSeparatedByString:@"#"];
69+
NSData *data = [[NSData alloc]initWithBase64EncodedString:arr_t[1] options:0];
70+
NSString *reStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
71+
if (!data) {
72+
reStr = arr_t[1];
73+
}
74+
MathModel *model = [[MathModel alloc]init];
75+
MathView *math = [[MathView alloc]initWithRenderCompleted:^(CGRect frame, NSInteger index) {
76+
MathModel *Model = model;
77+
Model.rect = frame;
78+
[arrNew replaceObjectAtIndex:index
79+
withObject:Model];
80+
weakSelf.renderCount ++;
81+
[weakSelf typesetWithOriArr:arr
82+
andMultiArr:arrNew];
83+
}
84+
withHTML:reStr
85+
MathRenderEngineType:self.type
86+
Index:i];
87+
model.mathView = math;
88+
math.ratio = 1.0;
89+
[self addSubview:math];
90+
}else if([str hasPrefix:@"image#"]){
91+
NSArray *arr_m = [str componentsSeparatedByString:@"#"];
92+
NSArray *urls = [arr_m[1] componentsSeparatedByString:@";"];
93+
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:urls[0]] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
94+
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
95+
NSUInteger imageIndex = [arr indexOfObject:[NSString stringWithFormat:@"image#%@;%@",imageURL.absoluteString,urls[1]]];
96+
if (image) {
97+
[arrNew replaceObjectAtIndex:imageIndex withObject:image];
98+
}else{
99+
[arrNew replaceObjectAtIndex:imageIndex withObject:@""];
100+
}
101+
weakSelf.renderCount ++;
102+
[weakSelf typesetWithOriArr:arr andMultiArr:arrNew];
103+
}];
104+
}else{
105+
[arrNew replaceObjectAtIndex:i withObject:str];
106+
self.renderCount ++;
107+
[self typesetWithOriArr:arr andMultiArr:arrNew];
108+
}
109+
}
110+
}
111+
112+
/**
113+
解析固定协议下的数据字符串为数组
114+
115+
@param str 符合协议的数据字符串
116+
@return 数据源数组
117+
*/
118+
- (NSArray *)parserString:(NSString *)str{
119+
NSArray *arr = [str componentsSeparatedByString:@"@"];
120+
NSLog(@"%@",arr);
121+
NSMutableArray *newArr = [NSMutableArray arrayWithArray:arr];
122+
for (NSInteger i = 0; i < newArr.count; i ++) {
123+
if ([newArr[i] hasPrefix:@"/math#"]||[newArr[i] hasPrefix:@"/image#"]) {
124+
NSString *newStr = [newArr[i] stringByReplacingOccurrencesOfString:@"/math#" withString:@""];
125+
NSString *newStr1 = [newStr stringByReplacingOccurrencesOfString:@"/image#" withString:@""];
126+
127+
[newArr replaceObjectAtIndex:i withObject:newStr1];
128+
}
129+
}
130+
NSLog(@"%@",newArr);
131+
return newArr;
132+
}
133+
134+
/**
135+
渲染操作
136+
137+
@param arr 需要渲染的元数组
138+
@param arrNew 经过数据处理的可变元数组(渲染对象)
139+
*/
140+
- (void)typesetWithOriArr:(NSArray *)arr andMultiArr:(NSMutableArray *)arrNew{
141+
if (self.renderCount == arr.count) {
142+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
143+
//生成属性字符串
144+
NSMutableAttributedString *text = [NSMutableAttributedString new];
145+
146+
for (id obj in arrNew) {
147+
if ([obj isKindOfClass:[NSString class]]) {
148+
NSString *atStr = (NSString *)obj;
149+
[text appendAttributedString:[[NSAttributedString alloc] initWithString:atStr]];
150+
}else if ([obj isKindOfClass:[MathModel class]]){
151+
MathModel *model = (MathModel *)obj;
152+
MathView *mathView = model.mathView;
153+
CGRect rect = model.rect;
154+
[mathView sizeToFit];
155+
NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:mathView contentMode:0 attachmentSize:rect.size alignToFont:_font alignment:1];
156+
[text appendAttributedString:attachText];
157+
}else if ([obj isKindOfClass:[UIImage class]]){
158+
UIImage *image = (UIImage *)obj;
159+
NSMutableAttributedString *attachText = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:0 attachmentSize:image.size alignToFont:_font alignment:1];
160+
[text appendAttributedString:attachText];
161+
}
162+
}
163+
text.yy_font = _font;
164+
text.yy_color = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
165+
//创建文本容器
166+
YYTextContainer *container = [YYTextContainer new];
167+
container.size = CGSizeMake(self.width, CGFLOAT_MAX);
168+
container.maximumNumberOfRows = 0;
169+
//生成排版结果
170+
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
171+
172+
dispatch_async(dispatch_get_main_queue(), ^{
173+
self.label.size = layout.textBoundingSize;
174+
self.label.textLayout = layout;
175+
self.callBack(self.label.height + 2,self.indexPath);
176+
});
177+
});
178+
}
179+
}
180+
181+
/**
182+
数据源字符串setter
183+
184+
@param testStr 数据源字符串
185+
*/
186+
- (void)setTestStr:(NSString *)testStr{
187+
WS(weakSelf);
188+
_testStr = testStr;
189+
NSArray *arr = [weakSelf parserString:weakSelf.testStr];
190+
[weakSelf typesetMathWithArray:arr];
191+
}
192+
193+
@end

MathView.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/**
20+
init
21+
22+
@param callBack self.Frame , element's index
23+
@param code HTMLString
24+
@param type MathRenderEngineType
25+
@param index element's index
26+
@return MathView instancetype
27+
*/
28+
- (instancetype)initWithRenderCompleted:(void(^)(CGRect frame,NSInteger index))callBack
29+
withHTML:(NSString *)code
30+
MathRenderEngineType:(MathRenderEngineType)type
31+
Index:(NSInteger)index;
32+
@end

0 commit comments

Comments
 (0)