Как изменить цвет NSString в drawAtPoint
У меня есть этот фрагмент кода, который рисует блок с односимвольной строкой на нем:
CGContextDrawImage(context, CGRectMake([blok getLocation].x * xunit, [blok getLocation].y * yunit, 40, 40), [blok getImage].CGImage);
[[blok getSymbol] drawAtPoint:CGPointMake([blok getLocation].x * xunit+15, [blok getLocation].y * yunit) withFont:[UIFont fontWithName:@"Helvetica" size:24]];
Он работает нормально, но я делал некоторые изменения макета, и теперь мне это нужно, чтобы нарисованная строка была белой. Использование методов настройки цвета заливки и цвета штрихов ничего не сделано. Есть ли другой способ сделать это?
Ответы
Ответ 1
Вы пробовали:
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor);
Например:
CGContextDrawImage(context, CGRectMake([blok getLocation].x * xunit, [blok getLocation].y * yunit, 40, 40), [blok getImage].CGImage);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), textColor);
[[blok getSymbol] drawAtPoint:CGPointMake([blok getLocation].x * xunit+15, [blok getLocation].y * yunit) withFont:[UIFont fontWithName:@"Helvetica" size:24]];
Ответ 2
Задайте цвет переднего плана в атрибутах и используйте функции draw withAttributes
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:
@[font, [UIColor whiteColor]]
forKeys:
@[NSFontAttributeName, NSForegroundColorAttributeName]];
[string drawInRect:frame withAttributes:attributes];
Ответ 3
Это то, что я использую для рисования меток:
- (void)_drawLabel:(NSString *)label withFont:(UIFont *)font forWidth:(CGFloat)width
atPoint:(CGPoint)point withAlignment:(UITextAlignment)alignment color:(UIColor *)color
{
// obtain current context
CGContextRef context = UIGraphicsGetCurrentContext();
// save context state first
CGContextSaveGState(context);
// obtain size of drawn label
CGSize size = [label sizeWithFont:font
forWidth:width
lineBreakMode:UILineBreakModeClip];
// determine correct rect for this label
CGRect rect = CGRectMake(point.x, point.y - (size.height / 2),
width, size.height);
// set text color in context
CGContextSetFillColorWithColor(context, color.CGColor);
// draw text
[label drawInRect:rect
withFont:font
lineBreakMode:UILineBreakModeClip
alignment:alignment];
// restore context state
CGContextRestoreGState(context);
}