0

您好,我有一个带有覆盖 drawRect 方法的自定义视图,用于绘制图形的条形图,代码是:

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);

    UIColor *barColor = [UIColor colorWithRed:226.0/255.0 green:178.0/255.0 blue:39.0/255.0 alpha:1.0];

    CGContextSetFillColorWithColor(context, barColor.CGColor);
    CGContextAddRect(context, rect);

    CGContextFillPath(context);
}

我如何在栏的顶部绘制一个字符串?我想降低矩形的高度,并在矩形的顶部(高度降低)用白色(与条形使用的颜色不同的颜色)绘制字符串。

问候

4

1 回答 1

0
CGPoint textPoint = CGPointMake((rect.size.width - textSize.width) / 2, 0);
CGContextShowTextAtPoint(context, textPoint.x, textPoint.y, text, strlen(text));
CGContextSetTextDrawingMode (context, kCGTextFillStroke);

计算 textSize 通过

textSize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]];
于 2013-08-14T15:06:52.537 回答