有没有办法使刻度线(主刻度线)成为UIImage(自定义图形)而不是文本? 我在我需要作为图标呈现的坐标轴中有一个值。 我没有看到任何代表,但也许有人知道这个诀窍?如何在Core Plot中为图标设置自定义刻度标记?
1
A
回答
2
轴标签可以包含任何CPTLayer
(它是Core Animation的CALayer
的直接子类)作为其内容。将图像设置为图层背景,并使用此图层构建自定义标签。几个Core Plot示例应用程序演示了自定义标签,尽管它们都使用文本标签。
您有您的加入自定义标签的图形两种选择:
使用
CPTAxisLabelingPolicyNone
标签策略。创建一个包含标签的NSSet
,并将其设置为坐标轴上的axisLabels
属性。如果您使用此功能,请记住除了自定义标签之外,您还必须提供主要和/或次要的勾选位置。使用任何其他标签策略来生成勾号位置并实施轴委托方法。在您的代表中,在提供的位置创建新标签并返回
NO
以禁止自动标签。
埃里克
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
相关问题
- 1. Core Plot的自定义轴标签
- 2. 如何在Core Plot中使用多个自定义标签?
- 3. 核心绘图,自定义标签和刻度标记
- 4. 如何在iOS中设置自定义标记图标与标记簇Swift
- 5. 如何为我当前的标记设置自定义标记?
- 6. 在X轴上自定义刻度标记和标签(Excel VBA)
- 7. 如何在nvd3中在Y轴上设置自定义刻度和标签
- 8. R - ggplot2 - 设置刻度标记间隔
- 9. 在JxMap中设置标记的自定义图标
- 10. 如何在pyplot中正好设置10个刻度标记?
- 11. 设置自定义图标
- 12. 如何刻度标记设置为连续GGPLOT2传奇边缘
- 13. 设置自定义标记图像会覆盖默认标记图标以及自定义图标
- 14. 在对数刻度上使用自定义刻度标记的x轴间距
- 15. 将图标设置为自定义QFontDialog
- 16. ř图轴刻度标记
- 17. 如何将自定义标记窗口设置为特定标记
- 18. 在Tkinter中标记刻度
- 19. 添加自定义刻度和标签
- 20. jqPlot自定义刻度标签
- 21. 在matplotlib中设置刻度标签
- 22. ALTER刻度标记
- 23. 如何在Android中将标记设置为Google地图标记?
- 24. 如何在Moodle主题设置中创建自定义标记?
- 25. 如何使用Core Plot设置图表宽度?
- 26. 轴刻度标记
- 27. 自定义标签不会显示[core-plot]
- 28. 将地图标记设置为自定义颜色Android
- 29. matplotlib中的刻度标记
- 30. 如何设置只有整数的x轴刻度标记?
您的回答让我相当吃惊,但我在CPLayer上绘制图标itselt时遇到了问题。 CPLayer正在绘制正确,但没有图像(图层的内容)。查看所用代码的问题更新。 – Lukasz
当您说“在提供的位置创建新标签”时,您的意思是“tickLocation”属性?我一直在我的代表基于委托(地点)的参数设置他们,但他们都安装在位置= 0. – Maverick