0

我正在使用以下方法在矩形中绘制一个字符串

- (void)drawRect:(CGRect)rect {
    .................................................
    .................................................
    for(NSString *titleString in self.titlesArray) {
        CGContextSaveGState(context);                                                 // Pushes a copy of the current graphics state onto the graphics state stack for the context.
        CGRect labelRect = CGRectMake((self.segmentWidth*i), yCoor, self.segmentWidth, self.font.pointSize);
        CGContextAddRect(context, labelRect);                                         // Adds a rectangular path to the current path.
        [titleString drawInRect:labelRect withFont:self.font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
        CGContextRestoreGState(context);
    }
}

结果是

在此处输入图像描述

我们可以看到字符串的默认颜色是黑色

问题:是否可以在 drawRect() 期间更改字符串的颜色以及如何将其存档。

4

2 回答 2

2

就像是:

[[UIColor blueColor] set];

将工作。如果您希望所有字符串都相同,请将其放在循环之前。

于 2012-10-19T21:25:42.217 回答
1

你可以使用这个:

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
于 2012-10-19T21:45:20.380 回答