CGContextSetStrokeColorWithColor为什么不将文本颜色设置为黑色? [英] Why doesn't CGContextSetStrokeColorWithColor set text colour to black?

查看:305
本文介绍了CGContextSetStrokeColorWithColor为什么不将文本颜色设置为黑色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





如果标出了圆点,则文本标签为黑色。但是,当图像被填充时,文本将变为点色。



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



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



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



with



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



我也删除了



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



产生的标签更干净。





感谢Ben。
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ p

这是原始代码

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

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

个扇区= 16;
uberAngle =(2.0 * PI)/扇区;

dotAngle = PI * -0.5; //在三点之前开始绘制0.5 PI弧度
endAngle = PI * 1.5; // 3点后停止绘制1.5 PI弧度

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

dotRadius = 20;
pointsFilled = FALSE;
AlternativeDots = TRUE; //交替填充和轮廓化

textOffset = 4; //为中心点文本

添加偏移量(dotCount = 1; dotCount< =扇区; dotCount ++)
{
//创建一个新的iOSCircle对象
iOSCircle * newCircle = [[iOSCircle alloc] init];

newCircle.circleRadius = dotRadius;
[self newPoint]; //查找下一个点的点坐标
dotPosition = CGPointMake(x,y); //创建点中心

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

newCircle.circleCentre = dotPosition; //将每个点放置在框架
上[totalCircles addObject:newCircle];
[self setNeedsDisplay];
}

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

for(iOSCircle * circle in totalCircles){
CGContextEOFillPath(上下文);
CGContextAddArc(context,circle.circleCentre.x,circle.circleCentre.y,circle.circleRadius,0.0,M_PI * 2.0,是);
//绘制圆圈

int paintThisDot = dotsFilled * alternateDots *!(dotCount%2); //绘制dotCount甚至是

NSLog(@ Dot%i Filled%i,dotCount,dotsFilled);
开关(paintThisDot){
情况1:
CGContextSetFillColorWithColor(context,[[[UIColor cyanColor] CGColor]);
CGContextDrawPath(context,kCGPathFillStroke);
休息时间;
默认值://绘制点轮廓
CGContextStrokePath(context);
休息时间;
}
CGContextClosePath(context);

[self newPoint]; //查找下一个点的点坐标

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

CGContextBeginPath(context);

//绘制标签

for(dotCount = 1; dotCount< =行业; dotCount ++)
{
//创建一个新的iOSCircle对象
iOSCircle * newCircle = [[iOSCircle alloc] init];
newCircle.circleRadius = dotRadius;
[self newPoint]; //查找下一个点的点坐标
dotPosition = CGPointMake(x,y); //为标签
使用点坐标[self autoLabel]; //打印标签
}

}



这是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); //设置相对于点中心的框边界[[NSString stringWithFormat:@%i,dotCount] drawInRect:boxBoundary withFont:[UIFont systemFontOfSize:24] lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter];}  

解决方案

使用 CGContextSetFillColorWithColor (填充)而不是 CGContextSetStrokeColorWithColor (描边),然后再绘制文本。


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];
}

解决方案

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

这篇关于CGContextSetStrokeColorWithColor为什么不将文本颜色设置为黑色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆