如何更改 UIFont 的颜色?

发布于 2024-10-10 07:03:40 字数 236 浏览 0 评论 0原文

我在网上搜索过,但找不到好的答案,尽管我确信这很简单。有人可以告诉我如何执行以下操作:制作一个名称为“Helvetica-Bold”、大小为 8.0、颜色为黑色的 UIFont。

我已经知道了名称和尺寸,但我不知道如何更改颜色:

UIFont *textFont = [UIFont fontWithName:@"Helvetica-Bold" size:8.0];

谢谢!

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百合的盛世恋 2024-10-17 07:03:40

UIFont 不包含/维护任何颜色信息,因此这是不可能的。通常,使用 UIFont 的控件(例如 UILabel)同时具有字体和 textColor 属性。因此,您可以设置标签的字体和颜色,如下所示:

myLabel.textColor = [UIColor blackColor];
myLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:8.0];

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:

myLabel.textColor = [UIColor blackColor];
myLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:8.0];
甜妞爱困 2024-10-17 07:03:40

如果您正在绘制文本,您可以使用以下功能;

[text drawAtPoint:CGPointMake(x, y) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:sizeOfFont], NSForegroundColorAttributeName:[UIColor redColor] }];

If you are drawing text you can use following function;

[text drawAtPoint:CGPointMake(x, y) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:sizeOfFont], NSForegroundColorAttributeName:[UIColor redColor] }];
心的位置 2024-10-17 07:03:40

如果您有机会在 OpenGL 中将字体渲染为纹理,您还可以使用 CGContext* 函数

假设您已经有一个有效的 cgcontext:

CGContextSelectFont(cgContext, "Arial", 24, kCGEncodingMacRoman);
CGContextSetRGBStrokeColor(cgContext, 1.0f, 1, 1, .5f) ;
CGContextShowTextAtPoint(cgContext, 20, 20, "HI THERE", strlen("HI THERE") ) ;

If by any chance you're rendering the font to a texture in OpenGL, you can also use the CGContext* functions,

Assuming you already have a valid cgcontext:

CGContextSelectFont(cgContext, "Arial", 24, kCGEncodingMacRoman);
CGContextSetRGBStrokeColor(cgContext, 1.0f, 1, 1, .5f) ;
CGContextShowTextAtPoint(cgContext, 20, 20, "HI THERE", strlen("HI THERE") ) ;
毁梦 2024-10-17 07:03:40

您还可以使用[myLabel setTextColor:[UIColor greyColor]];

You can also use [myLabel setTextColor:[UIColor grayColor]];.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文