如何将标签添加到情节
问题描述:
我有核心绘图表是这样的:如何将标签添加到情节
graph http://img413.imageshack.us/img413/8347/img0157h.jpg
我怎样才能标签添加到每个这些地块。我已经看到了一些关于向图中添加CPTextLayer的参考,但无法弄清楚如何去做。有人能指点我吗?
答
我能三个标签添加到图形像这样的顶部:
CPTextStyle *textStyle1 = [CPTextStyle textStyle];
textStyle1.color = [CPColor blueColor];
textStyle1.fontSize = 14;
CPTextStyle *textStyle2 = [CPTextStyle textStyle];
textStyle2.color = [CPColor greenColor];
textStyle2.fontSize = 14;
CPTextStyle *textStyle3 = [CPTextStyle textStyle];
textStyle3.color = [CPColor redColor];
textStyle3.fontSize = 14;
CPTextLayer *layer1 = [[[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"text1"]] autorelease];
CPTextLayer *layer2 = [[[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"text2"]] autorelease];
CPTextLayer *layer3 = [[[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"text3"]] autorelease];
layer1.bounds = CGRectMake(-75,2,20,10);
layer1.textStyle = textStyle1;
layer2.bounds = CGRectMake(-130,2, 20,10);
layer2.textStyle = textStyle2;
layer3.bounds = CGRectMake(-190,2, 20,10);
layer3.textStyle = textStyle3;
CPLayerHostingView * newView = [[CPLayerHostingView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
newView.hostedLayer = graph;
NSInteger count = [graph.sublayers count];
[graph insertSublayer:layer1 atIndex: count + 1];
[graph insertSublayer:layer2 atIndex: count + 2];
[graph insertSublayer:layer3 atIndex: count + 3];
self.view = newView;
[newView release];
谢谢您的回答它真的帮助我,但唯一的问题是,当我试图设置颜色和字体soze ,它给我一个错误“错误:对象无法设置 - 只读属性或没有设置找到” 我必须做点什么 – Pooja 2011-05-18 08:44:59
我不得不使用CPMutableTextStyle而不是CPTextStyle来使它工作。 – 2011-07-19 17:07:43
看起来图书馆的名称已更改。如果海报更新了代码以将CP更改为CPT,那将会很棒。 – 2011-08-03 21:21:15