为什么 CGContextSetStrokeColorWithColor 不将文本颜色设置为黑色?

Why doesn't CGContextSetStrokeColorWithColor set text colour to black?

我使用 iOS Core Graphics 在点圆弧前绘制了文本标签。我试图理解为什么 CGContextSetStrokeColorWithColor 在下面的两个示例中没有将文本颜色设置为黑色。

如果点被勾勒出来,文本标签是黑色的。但是当图像被填充时,文本会变为点颜色。

编辑:*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ben Zotto 的建议帮助解决了这个问题。在下面的原始代码中,解决方案是替换

CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);

CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);

我也删除了

CGContextSetShadowWithColor(context, CGSizeMake(-3 , 2), 4.0, [UIColor clearColor].CGColor);

导致标签更加简洁。

谢谢本。 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这里是原代码

- (void)rosette                  {
    context         = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 0.5);

    uberX           =   160;
    uberY           =   240;
    uberRadius      =   52;

    sectors         =   16;
    uberAngle       =   (2.0 * PI) / sectors;

    dotAngle        =   PI * -0.5;              // start drawing 0.5 PI radians before 3 o'clock
    endAngle        =   PI *  1.5;              // stop drawing 1.5 PI radians after 3 o'clock

NSLog(@"%f %f %f %f %f", uberX, uberY, uberRadius, uberAngle, dotAngle);

    dotRadius       =   20;
    dotsFilled      =   FALSE;
    alternateDots   =   TRUE;                   // alternately filled and outlined

    textOffset      =   4;                      // offset added to centre text

    for (dotCount   = 1; dotCount <= sectors; dotCount++)
    {
        // Create a new iOSCircle Object
        iOSCircle *newCircle    = [[iOSCircle alloc] init];

        newCircle.circleRadius  = dotRadius;
        [self newPoint];                        // find point coordinates for next dot
        dotPosition = CGPointMake(x,y);         // create dot centre

        NSLog(@"Circle%i: %@", dotCount, NSStringFromCGPoint(dotPosition));

        newCircle.circleCentre  = dotPosition;  // place each dot on the frame
        [totalCircles addObject:newCircle];
        [self setNeedsDisplay];
    }

    CGContextSetShadowWithColor(context, CGSizeMake(-3 , 2), 4.0, [UIColor clearColor].CGColor);
    dotCount = 1;

    for (iOSCircle *circle in totalCircles) {
        CGContextEOFillPath (context);
        CGContextAddArc(context, circle.circleCentre.x, circle.circleCentre.y, circle.circleRadius, 0.0, M_PI * 2.0, YES);
        // draw the circles

        int paintThisDot = dotsFilled * alternateDots * !(dotCount % 2); // paint if dotCount is even

        NSLog(@"Dot %i Filled %i ", dotCount, dotsFilled);
        switch (paintThisDot) {
        case 1:
                CGContextSetFillColorWithColor(context, [[UIColor cyanColor] CGColor]);
                CGContextDrawPath(context, kCGPathFillStroke);
            break;
        default:                                // draw dot outline
                CGContextStrokePath(context);
            break;
        }
            CGContextClosePath(context);

        [self newPoint];                        // find point coordinates for next dot

        dotCount++;
    }
CGContextSetShadowWithColor(context, CGSizeMake(-3, 2), 4.0, [UIColor grayColor].CGColor);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);

CGContextBeginPath(context);

// draw labels

for (dotCount   = 1; dotCount <= sectors; dotCount++)
{
    // Create a new iOSCircle Object
    iOSCircle *newCircle    = [[iOSCircle alloc] init];
    newCircle.circleRadius  = dotRadius;
    [self newPoint];                            // find point coordinates for next dot
    dotPosition = CGPointMake(x,y);             // use point coordinates for label
    [self autoLabel];                           // prints labels
}

}

这里是 newPoint

的方法

- (void)newPoint {
dotAngle = dotAngle + uberAngle;
x = uberX + (uberRadius * 2 * cos(dotAngle));
y = uberY + (uberRadius * 2 * sin(dotAngle));
    
    NSLog(@"%i %f %f %f", dotCount, dotAngle, endAngle, uberAngle);
}

以及autoLabel的方法

- (void)autoLabel {
    boxBoundary = CGRectMake(x-dotRadius, y-dotRadius+textOffset, dotRadius*2, dotRadius*2);   // set box boundaries relative to dot centre
    [[NSString stringWithFormat:@"%i",dotCount] drawInRect:boxBoundary withFont:[UIFont systemFontOfSize:24] lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter];
}

在绘制文本之前使用 CGContextSetFillColorWithColor(填充)而不是 CGContextSetStrokeColorWithColor(描边)。