我有代码在 UIView 子类的 drawRect 方法中进行自定义渲染。我正在尝试使用 [NSString drawInRect]
渲染文本,效果很好,但它始终显示为白色。经过多次谷歌搜索和浏览,所以我无法找到如何更改文本颜色的方法。任何建议都会很棒。
编辑 下面是我的 drawRect 实现中的代码片段
if(self.state != UIControlStateHighlighted)
{
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, color);
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3.0, shadowColor);
CGContextAddPath(context, path);
CGContextFillPath(context);
[title drawInRect:rect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
CGContextRestoreGState(context);
}
最佳答案
在绘制文本之前,您必须设置颜色。试试这个,
[[UIColor blackColor] set]; [title drawInRect:rect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
关于iOS 在 View 的 drawrect 中设置文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505626/