dullgrass

fix bug

@@ -39,8 +39,8 @@ @@ -39,8 +39,8 @@
39 39
40 - (void)setGrade:(float)grade 40 - (void)setGrade:(float)grade
41 { 41 {
42 -// NSLog(@"New garde %f",grade); 42 + NSLog(@"New garde %f",grade);
43 - 43 + _grade = grade;
44 CGFloat startPosY = (1 - grade) * self.frame.size.height; 44 CGFloat startPosY = (1 - grade) * self.frame.size.height;
45 45
46 UIBezierPath *progressline = [UIBezierPath bezierPath]; 46 UIBezierPath *progressline = [UIBezierPath bezierPath];
@@ -180,7 +180,7 @@ @@ -180,7 +180,7 @@
180 _textLayer = [[CATextLayer alloc]init]; 180 _textLayer = [[CATextLayer alloc]init];
181 [_textLayer setString:@"0"]; 181 [_textLayer setString:@"0"];
182 [_textLayer setAlignmentMode:kCAAlignmentCenter]; 182 [_textLayer setAlignmentMode:kCAAlignmentCenter];
183 - [_textLayer setForegroundColor:[[UIColor blackColor] CGColor]]; 183 + [_textLayer setForegroundColor:[[UIColor colorWithRed:178/255.0 green:178/255. blue:178/255.0 alpha:1.0] CGColor]];
184 } 184 }
185 185
186 return _textLayer; 186 return _textLayer;
@@ -188,23 +188,29 @@ @@ -188,23 +188,29 @@
188 188
189 -(void)setGradeFrame:(CGFloat)grade startPosY:(CGFloat)startPosY 189 -(void)setGradeFrame:(CGFloat)grade startPosY:(CGFloat)startPosY
190 { 190 {
191 - CGFloat textheigt = self.bounds.size.width; 191 + CGFloat textheigt = self.bounds.size.height*self.grade;
192 - CGFloat textWidth = self.bounds.size.width;  
193 - CGFloat textStartPosY;  
194 -  
195 192
196 - if (startPosY < textheigt) { 193 + CGFloat topSpace = self.bounds.size.height * (1-self.grade);
197 - textStartPosY = startPosY; 194 + CGFloat textWidth = self.bounds.size.width;
198 - }  
199 - else {  
200 - textStartPosY = startPosY - textheigt;  
201 - }  
202 195
203 [_chartLine addSublayer:self.textLayer]; 196 [_chartLine addSublayer:self.textLayer];
204 - [self.textLayer setFontSize:textheigt/2]; 197 + [self.textLayer setFontSize:18.0];
205 198
206 [self.textLayer setString:[[NSString alloc]initWithFormat:@"%0.f",grade*self.maxDivisor]]; 199 [self.textLayer setString:[[NSString alloc]initWithFormat:@"%0.f",grade*self.maxDivisor]];
207 - [self.textLayer setFrame:CGRectMake(0, textStartPosY, textWidth, textheigt)]; 200 +
  201 + CGSize size = CGSizeMake(320,2000); //设置一个行高上限
  202 + NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18.0]};
  203 + size = [self.textLayer.string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
  204 + float verticalY ;
  205 +
  206 + if (size.height>=textheigt) {
  207 +
  208 + verticalY = topSpace - size.height;
  209 + } else {
  210 + verticalY = topSpace + (textheigt-size.height)/2.0;
  211 + }
  212 +
  213 + [self.textLayer setFrame:CGRectMake((textWidth-size.width)/2.0,verticalY, size.width,size.height)];
208 self.textLayer.contentsScale = [UIScreen mainScreen].scale; 214 self.textLayer.contentsScale = [UIScreen mainScreen].scale;
209 215
210 } 216 }
@@ -212,6 +218,15 @@ @@ -212,6 +218,15 @@
212 - (void)setIsNegative:(BOOL)isNegative{ 218 - (void)setIsNegative:(BOOL)isNegative{
213 if (isNegative) { 219 if (isNegative) {
214 [self.textLayer setString:[[NSString alloc]initWithFormat:@"-%0.f",_grade*self.maxDivisor]]; 220 [self.textLayer setString:[[NSString alloc]initWithFormat:@"-%0.f",_grade*self.maxDivisor]];
  221 +
  222 + CGSize size = CGSizeMake(320,2000); //设置一个行高上限
  223 + NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18.0]};
  224 + size = [self.textLayer.string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
  225 + CGRect frame = self.textLayer.frame;
  226 + frame.origin.x = (self.bounds.size.width - size.width)/2.0;
  227 + frame.size = size;
  228 + self.textLayer.frame = frame;
  229 +
215 CABasicAnimation* rotationAnimation; 230 CABasicAnimation* rotationAnimation;
216 rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 231 rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
217 rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI]; 232 rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI];
@@ -113,7 +113,7 @@ @@ -113,7 +113,7 @@
113 113
114 [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen]]; 114 [self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen]];
115 // Adding gradient 115 // Adding gradient
116 - self.barChart.barColorGradientStart = [UIColor blueColor]; 116 +// self.barChart.barColorGradientStart = [UIColor blueColor];
117 117
118 [self.barChart strokeChart]; 118 [self.barChart strokeChart];
119 119