-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathLegendView.m
149 lines (119 loc) · 5.84 KB
/
LegendView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// LegendView.m
// dr-charts
//
// Created by DHIREN THIRANI on 4/1/16.
// Copyright © 2016 Product. All rights reserved.
//
#import "LegendView.h"
#import "DrConstants.h"
@implementation LegendView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.legendArray = [[NSMutableArray alloc] init];
self.font = [UIFont systemFontOfSize:12];
}
return self;
}
- (void)createLegend{
CGFloat height = INNER_PADDING;
if (self.legendViewType == LegendTypeVertical) {
for (LegendDataRenderer *legendData in self.legendArray) {
if (legendData.legendText.length) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, height, LEGEND_VIEW, LEGEND_VIEW)];
[view setBackgroundColor:legendData.legendColor];
[self addSubview:view];
NSAttributedString *attrString = [LegendView getAttributedString:legendData.legendText withFont:self.font];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(WIDTH(self) - WIDTH(view), MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;
UILabel *label = [[UILabel alloc] init];
[label setNumberOfLines:0];
[label setTextColor:self.textColor];
[label setAttributedText:attrString];
[label setFrame:CGRectMake(AFTER(view) + OFFSET_PADDING, height, WIDTH(self) - WIDTH(view), size.height)];
[self addSubview:label];
if (size.height > LEGEND_VIEW) {
height = height + size.height + INNER_PADDING;
}
else{
height = height + LEGEND_VIEW + INNER_PADDING;
}
}
}
CGRect frame = self.frame;
frame.size.height = height;
[self setFrame:frame];
}
else if (self.legendViewType == LegendTypeHorizontal){
CGFloat width = 0;
CGFloat x = 0;
for (LegendDataRenderer *legendData in self.legendArray) {
if (legendData.legendText.length) {
NSAttributedString *attrString = [LegendView getAttributedString:legendData.legendText withFont:self.font];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(WIDTH(self) - LEGEND_VIEW, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;
width += LEGEND_VIEW + size.width + INNER_PADDING + OFFSET_PADDING;
if (width >= WIDTH(self)) {
height += LEGEND_VIEW + INNER_PADDING;
width = LEGEND_VIEW + size.width + INNER_PADDING + OFFSET_PADDING;
x = 0;
}
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, height, LEGEND_VIEW, LEGEND_VIEW)];
[view setBackgroundColor:legendData.legendColor];
[self addSubview:view];
UILabel *label = [[UILabel alloc] init];
[label setNumberOfLines:0];
[label setTextColor:self.textColor];
[label setAttributedText:attrString];
[label setFrame:CGRectMake(AFTER(view) + OFFSET_PADDING, height, size.width, size.height)];
[self addSubview:label];
x = width;
}
}
height += LEGEND_VIEW + INNER_PADDING;
CGRect frame = self.frame;
frame.size.height = height;
[self setFrame:frame];
}
}
+ (NSMutableAttributedString *)getAttributedString:(NSString *)tinyText withFont:(UIFont *)font {
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:tinyText];
[attrString addAttributes:@{NSFontAttributeName:font}
range:NSMakeRange(0, tinyText.length)];
return attrString;
}
+ (CGFloat)getLegendHeightWithLegendArray:(NSMutableArray *)legendArray legendType:(LegendType)type withFont:(UIFont *)font width:(CGFloat)viewWidth{
CGFloat height = 0;
height += INNER_PADDING;
if (type == LegendTypeVertical) {
for (LegendDataRenderer *legendData in legendArray){
NSAttributedString *attrString = [LegendView getAttributedString:legendData.legendText withFont:font];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(viewWidth - LEGEND_VIEW, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;
if (size.height > LEGEND_VIEW) {
height = height + size.height + INNER_PADDING;
}
else{
height = height + LEGEND_VIEW + INNER_PADDING;
}
}
}
else if (type == LegendTypeHorizontal){
CGFloat width = 0;
CGFloat x = 0;
for (LegendDataRenderer *legendData in legendArray){
NSAttributedString *attrString = [LegendView getAttributedString:legendData.legendText withFont:font];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(viewWidth - LEGEND_VIEW, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;
x = width;
width += LEGEND_VIEW + size.width + INNER_PADDING + OFFSET_PADDING;
if (width >= viewWidth) {
height = height + LEGEND_VIEW + INNER_PADDING;
width = LEGEND_VIEW + size.width + INNER_PADDING + OFFSET_PADDING;
x = 0;
}
}
height += LEGEND_VIEW + INNER_PADDING;
}
return height;
}
@end
@implementation LegendDataRenderer
@end