ios 你如何改变 UIFont 的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4585715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you change the color of a UIFont?
提问by CodeGuy
I've searched the web and I can't get a good answer, although I'm sure this is very simple. Can someone please show me how to do the following: Make a UIFont of name "Helvetica-Bold", size 8.0, and color of black.
我在网上搜索过,但我无法得到一个好的答案,尽管我确信这很简单。有人可以告诉我如何执行以下操作:制作名称为“Helvetica-Bold”、大小为 8.0 且颜色为黑色的 UIFont。
I've got the name and size, but I can't figure out how to change the color:
我知道了名称和尺寸,但不知道如何更改颜色:
UIFont *textFont = [UIFont fontWithName:@"Helvetica-Bold" size:8.0];
Thanks!
谢谢!
回答by theChrisKent
UIFont does not contain/maintain any color information, so this is not possible. In general, controls that use UIFont such as a UILabel have both a font and a textColor property. So you would set a label's font and it's color like this:
UIFont 不包含/维护任何颜色信息,因此这是不可能的。通常,使用 UIFont 的控件(例如 UILabel)同时具有 font 和 textColor 属性。因此,您可以设置标签的字体和颜色,如下所示:
myLabel.textColor = [UIColor blackColor];
myLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:8.0];
回答by Oktay
If you are drawing text you can use following function;
如果您正在绘制文本,您可以使用以下功能;
[text drawAtPoint:CGPointMake(x, y) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:sizeOfFont], NSForegroundColorAttributeName:[UIColor redColor] }];
回答by bobobobo
If by any chance you're rendering the font to a texture in OpenGL, you can also use the CGContext* functions,
如果您在 OpenGL 中将字体渲染为纹理,您还可以使用CGContext* 函数,
Assuming you already have a valid cgcontext:
假设您已经有一个有效的 cgcontext:
CGContextSelectFont(cgContext, "Arial", 24, kCGEncodingMacRoman);
CGContextSetRGBStrokeColor(cgContext, 1.0f, 1, 1, .5f) ;
CGContextShowTextAtPoint(cgContext, 20, 20, "HI THERE", strlen("HI THERE") ) ;
回答by NiKKi
You can also use [myLabel setTextColor:[UIColor grayColor]];
.
您也可以使用[myLabel setTextColor:[UIColor grayColor]];
.