gpt4 book ai didi

iphone - 核心图 CPTextLayer : alignment of text with background image as background color

转载 作者:行者123 更新时间:2023-12-03 20:54:37 25 4
gpt4 key购买 nike

我编写了以下代码来显示条形图注释。显示注释,但具有意外的对齐/位置。 CPTextLayer 的文本显示在框架的左上角。我试图在中心展示它。我使用 CPTextLayer 的背景图像作为背景颜色。请看一下代码-(symbolTextAnnotation是接口(interface)中声明的CPLayerAnnotation对象)

-(void)barPlot:(CPBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
{
CPXYGraph* graph = (CPXYGraph*)plot.graph;
NSNumber *value = [self numberForPlot:plot field:CPBarPlotFieldBarTip recordIndex:index];

if ( symbolTextAnnotation ) {
[graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation = nil;
}

// Setup a style for the annotation
CPMutableTextStyle *hitAnnotationTextStyle = [CPMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPColor whiteColor];
hitAnnotationTextStyle.fontSize = 9.0f;
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";


// Determine point of symbol in plot coordinates
NSNumber *x = [NSNumber numberWithInt:index+1];
NSNumber *y = [NSNumber numberWithInt:0];

NSArray *anchorPoint = [NSArray arrayWithObjects:y, x, nil];

// Add annotation
// First make a string for the y value
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:value];

// Now add the annotation to the plot area
UIImage *annotationBG = [UIImage imageNamed:@"annotation-bg.png"];
CPTextLayer *textLayer = [[[CPTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle] autorelease];

textLayer.backgroundColor = [UIColor colorWithPatternImage:annotationBG].CGColor;
textLayer.frame = CGRectMake(0, 0, annotationBG.size.width, annotationBG.size.height);


symbolTextAnnotation = [[CPPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer = textLayer;
symbolTextAnnotation.displacement = CGPointMake(-18.0f, 3.0f);

[graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
}

最佳答案

CPTextLayer 根据其文本大小调整自身大小。设置文本后调整其大小会导致绘图问题。您可以制作另一个 CPLayer,在其上设置背景,使您的 CPTextLayer 成为子图层,然后将其放置在您想要的位置。

displacement 属性与 contentAnchorPoint 属性配合使用来定位注释。 contentAnchorPoint 是内容层的核心动画 anchor 。位移是该点相对于注释的anchorPlotPoint 的像素偏移。例如,如果您希望注释以anchorPlotPoint为中心,请将contentAnchorPoint设置为CGPointMake(0.5, 0.5),并将位移设置为CGPointZero

关于iphone - 核心图 CPTextLayer : alignment of text with background image as background color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5888993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com