[英]How to change color of NSString in 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]];
它工作正常,但我一直在做一些布局更改,现在我需要它,以便绘制的字符串将是白色的。 使用设置填充颜色和笔触颜色的方法没有做任何事情。 还有其他方法可以做到这一点吗?
在属性中设置foregroundcolor,并使用draw withAttributes函数
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:
@[font, [UIColor whiteColor]]
forKeys:
@[NSFontAttributeName, NSForegroundColorAttributeName]];
[string drawInRect:frame withAttributes:attributes];
你有没有尝试过:
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]];
这是我用于绘制标签的内容:
- (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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.