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

时间:2015-02-01 11:46:51

标签: ios objective-c core-graphics

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

labels are black when all circles are outlined

如果圆点有轮廓,则文字标签为黑色。但是当图像被填充时,文本会变为点颜色。

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

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



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






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




我也删除了



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




产生更清洁的标签。

fixed image

谢谢Ben。 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这是原始代码

- (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的方法

&#13;
&#13;
- (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);
}
&#13;
&#13;
&#13;

autoLabel的方法

&#13;
&#13;
- (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];
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在绘制文字之前,请先使用CGContextSetFillColorWithColor(填充)代替CGContextSetStrokeColorWithColor(笔画)。