我的设备有错误。在IOS模拟器中一切都好。我想在设备上正确显示文本层。
这张来自设备的图片。
这张来自IOS模拟器的图片
将文本层添加到图形的代码
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
if ( symbolTextAnnotation ) {
[self.graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation = nil;
}
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 14.0f;
hitAnnotationTextStyle.fontName = @"Helvetica";
NSNumber *x = [[self.plotDataFact objectAtIndex:index] valueForKey:@"x"];
NSNumber *y = [[self.plotDataFact objectAtIndex:index] valueForKey:@"y"];
NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init] ;
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:[NSNumber numberWithFloat:[y floatValue] * graphYStep]];
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle];
symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:self.graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer = textLayer;
textLayer.borderColor = [[UIColor whiteColor] CGColor];
textLayer.borderWidth = 0.5;
textLayer.cornerRadius = 4;
textLayer.backgroundColor = [[UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1.0] CGColor];
symbolTextAnnotation.displacement = CGPointMake(0.0f, 20.0f);
[self.graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
}