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 に答える