简体   繁体   English

UILabel 的字体颜色没有改变

[英]Font color for UILabel not changing

I am trying to display numbers on a UILabel with a bold black font and size 50.我正在尝试使用粗体黑色字体和大小 50 在 UILabel 上显示数字。

After some failed attempts I just realized that no matter what color I set the font to, it always gets set to lightGray.经过一些失败的尝试后,我才意识到无论我将字体设置为什么颜色,它总是设置为 lightGray。 Is there something else I need to do other than the below?除了下面的内容,我还有什么需要做的吗?

[DisplayLabel setFont:[UIFont fontWithName:[NSString stringWithUTF8String:"HelveticaNeue-Bold"] size:50]];
DisplayLabel.textColor = [UIColor brownColor];
DisplayLabel.textAlignment = NSTextAlignmentCenter;

I am adding the label using the storyboard to a view.我正在使用故事板将标签添加到视图中。

I encountered the same issue but it was cause by setting a custom color in the storyboard.我遇到了同样的问题,但这是由于在情节提要中设置自定义颜色造成的。 Apparently you must have the color attribute set to default in order to change it programatically.显然,您必须将颜色属性设置为default才能以编程方式更改它。 I found this true with all UIView s.我发现所有UIView都是如此。

Make sure your label's behavior like that.确保您的标签的行为是这样的。在此处输入图片说明

I was trying change programtelly the textcolor of a UILabel in a UITabelViewCell in cellForRowAt.我正在尝试以编程方式更改 cellForRowAt 中 UITabelViewCell 中 UILabel 的文本颜色。 And the label just change the textcolor after being reloaded.并且标签只是在重新加载后更改文本颜色。

I could change the label textColor in the beggining after change the TEXTCOLOR IN STORYBOARD TO DEFAULT and i change it programatelly.在将 STORYBOARD 中的 TEXTCOLOR 更改为 DEFAULT 后,我可以在开始时更改标签 textColor 并以编程方式更改它。

Yeah, that bug is pretty annoying.是的,这个bug很烦人。 Just relaunch the Xcode and it will work good只需重新启动 Xcode,它就会运行良好

I got this to work for a table or collection view cell by setting the color first to a system color, then to my custom color in cellForRowAtIndexPath eg通过首先将颜色设置为系统颜色,然后将其设置为我在 cellForRowAtIndexPath 中的自定义颜色,我使它适用于表格或集合视图单元格,例如

myLabel.textColor = .black
myLabel.textColor = UIColor(named: "My custom color")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM