如何在 Xcode 中更改标签文本的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17838798/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:51:51  来源:igfitidea点击:

How to change the color of the text of a label in Xcode

xcodeuilabel

提问by dragos

I am trying to change the text color of a label in Xcode IB. But each time, the background gets changed as well which is pretty annoying.

我正在尝试更改 Xcode IB 中标签的文本颜色。但每次,背景也会改变,这很烦人。

http://screencast.com/t/XgyqQrLe4cmV

http://screencast.com/t/XgyqQrLe4cmV

What is the correct way to change onlythe text color?

更改文本颜色的正确方法是什么?

回答by MZimmerman6

There should be a text color box in interface builder, but you can also do it through code.

界面构建器中应该有一个文本颜色框,但您也可以通过代码来完成。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,100)]
label.textColor = [UIColor redColor];

Or just use the reference that you already have to the text label. Make sure it is an IBOutlet and linked in interface builder though if you added it there.

或者只是使用您已经拥有的对文本标签的引用。如果你在那里添加它,请确保它是一个 IBOutlet 并在界面构建器中链接。

回答by Wain

When you click the color box in the UI you will see it get selected. If you don't see it get selected, click it again. It's when it doesn't get selected that the colour changes the background instead (which seems to be the default selection if you keep the color picker open).

当您单击 UI 中的颜色框时,您将看到它被选中。如果您没有看到它被选中,请再次单击它。当它没有被选择时,颜色会改变背景(如果你保持颜色选择器打开,这似乎是默认选择)。

回答by Brian

Also you can change the color to many other colors: Apple has quite a few convenience methods for this, say you wanted green text then you can use the greenColor convenience method:

您也可以将颜色更改为许多其他颜色:Apple 为此提供了很多便利方法,假设您想要绿色文本,那么您可以使用 greenColor 便利方法:

label.textColor = [UIColor greenColor];

There are many options, here are the other convenience methods I found in "UIColor.h":

有很多选项,这里是我在“UIColor.h”中找到的其他便捷方法:

+ (UIColor *)blackColor;      // 0.0 white 
+ (UIColor *)darkGrayColor;   // 0.333 white 
+ (UIColor *)lightGrayColor;  // 0.667 white 
+ (UIColor *)whiteColor;      // 1.0 white 
+ (UIColor *)grayColor;       // 0.5 white 
+ (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB 
+ (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB 
+ (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB 
+ (UIColor *)cyanColor;       // 0.0, 1.0, 1.0 RGB 
+ (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB 
+ (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB 
+ (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB 
+ (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB 
+ (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB 
+ (UIColor *)clearColor;      // 0.0 white, 0.0 alpha 

Alos if you wanted to color your label with a custom color not found in the above convenience methods like say Hot Pink we could do this: 255-105-180

如果您想使用上述便捷方法中没有的自定义颜色(例如 Hot Pink)为标签着色,我们可以这样做:255-105-180

label.textColor = [UIColor colorWithRed:(255.0/255.0) green:(105.0/255.0) blue:(180.0/255.0) alpha:1];//nice

RGB code for HOT pinkI found here: http://www.tayloredmktg.com/rgb/

我在这里找到了热粉色的RGB 代码:http: //www.tayloredmktg.com/rgb/

Also notice that I divide the RGB values by 255 to normalize them between 0 and 1. see this link for more info on that: Using [UIColor colorWithRed:green:blue:alpha:] doesn't work with UITableView seperatorColor?

另请注意,我将 RGB 值除以 255 以将它们在 0 和 1 之间标准化。有关更多信息,请参阅此链接:使用 [UIColor colorWithRed:green:blue:alpha:] 不适用于 UITableView seperatorColor?

I tested this out in Xcode 5, Deployment Target 7.1 and I got HOT PINK text! Now I'm ready for that Valentines app. try it your self :)

我在 Xcode 5, Deployment Target 7.1 中对此进行了测试,得到了 HOT PINK 文本!现在我已经准备好使用情人节应用程序了。自己试试 :)

回答by Javier Calatrava Llavería

As simple as:

就这么简单:

[self.lblValue setTextColor:[UIColor redColor]];