2011-07-19 51 views

回答

2

轴标签可以包含任何CPTLayer(它是Core Animation的CALayer的直接子类)作为其内容。将图像设置为图层背景,并使用此图层构建自定义标签。几个Core Plot示例应用程序演示了自定义标签,尽管它们都使用文本标签。

您有您的加入自定义标签的图形两种选择:

  1. 使用CPTAxisLabelingPolicyNone标签策略。创建一个包含标签的NSSet,并将其设置为坐标轴上的axisLabels属性。如果您使用此功能,请记住除了自定义标签之外,您还必须提供主要和/或次要的勾选位置。

  2. 使用任何其他标签策略来生成勾号位置并实施轴委托方法。在您的代表中,在提供的位置创建新标签并返回NO以禁止自动标签。

埃里克

+0

您的回答让我相当吃惊,但我在CPLayer上绘制图标itselt时遇到了问题。 CPLayer正在绘制正确,但没有图像(图层的内容)。查看所用代码的问题更新。 – Lukasz

+0

当您说“在提供的位置创建新标签”时,您的意思是“tickLocation”属性?我一直在我的代表基于委托(地点)的参数设置他们,但他们都安装在位置= 0. – Maverick

3

接受Eric的问题,并感谢他,我为他提供建议的解决方案运行的代码。 也许它可以帮助别人:

if (yAxisIcons) { 


    int custonLabelsCount = [self.yAxisIcons count]; 

    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:custonLabelsCount]; 

    for (NSUInteger i = 0; i < custonLabelsCount; i++) { 

     NSNumber *tickLocation = [NSNumber numberWithInt:i]; 
     NSString *file = [yAxisIcons objectAtIndex:i]; 
     UIImage *icon = [UIImage imageNamed:file]; 

     CPImageLayer *layer; // My custom CPLayer subclass - see code below 

      CGFloat nativeHeight = 1; 
      CGFloat nativeWidth = 1; 


     if (icon) { 

      layer = [[CPImageLayer alloc] initWithImage:icon]; 
      nativeWidth = 20;//CGImageGetWidth(icon.CGImage); 
      nativeHeight = 20;//CGImageGetHeight(icon.CGImage); 
      //layer.contents = (id)icon.CGImage; 

      if (nativeWidth > biggestCustomIconWidth) { 
       biggestCustomIconWidth = nativeWidth; 
      } 

     }else{ 
      layer = [[CPImageLayer alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; 
     } 

      CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight); 

      layer.frame = startFrame; 
      layer.backgroundColor = [UIColor clearColor].CGColor; 
      CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithContentLayer:layer]; 
      newLabel.tickLocation = [tickLocation decimalValue]; 
      newLabel.offset = x.labelOffset + x.majorTickLength; 
      [customLabels addObject:newLabel]; 
      [newLabel release]; 
      [layer release]; 

    } 

    y.axisLabels = [NSSet setWithArray:customLabels]; 

} 

CPImageLayer.h

#import "CPLayer.h" 

@interface CPImageLayer : CPLayer { 

    UIImage *_image; 

} 
-(id)initWithImage:(UIImage *)image; 
@end 

CPImageLayer.m

#import "CPImageLayer.h" 
#import "CPLayer.h" 

@implementation CPImageLayer 

-(void)dealloc{ 

    [_image release]; 
    [super dealloc]; 
} 
-(id)initWithImage:(UIImage *)image{ 

    CGRect f = CGRectMake(0, 0, image.size.width, image.size.height); 

    if (self = [super initWithFrame:f]) { 

     _image = [image retain]; 
    } 

    return self; 

} 

-(void)drawInContext:(CGContextRef)ctx{ 

    CGContextDrawImage(ctx, self.bounds, _image.CGImage); 

} 
@end 

享受

+0

谢谢,工作很好! – Maverick