Why doesn't CGContextSetStrokeColorWithColor set text colour to black?

纵饮孤独 提交于 2019-12-11 03:04:02

问题


I have drawn text labels in front of a circular arc of dots using iOS Core Graphics. I am trying to understand why CGContextSetStrokeColorWithColor does not set text colour to black in the two examples below.

Text labels are black if the dots are outlined. But when images are filled the text changes to the dot colour.

EDIT: *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The suggestion of Ben Zotto helped to resolved this issue. In the original code below the solution was to replace

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

with

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

I also removed

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

resulting in a much cleaner label.

Thanks Ben. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the original code

- (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
}

}

And here is the method for 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);
}

And the method for 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];
}

回答1:


Use CGContextSetFillColorWithColor (Fill) instead of CGContextSetStrokeColorWithColor (Stroke) before you draw the text.



来源:https://stackoverflow.com/questions/28262330/why-doesnt-cgcontextsetstrokecolorwithcolor-set-text-colour-to-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!