objective-c - 无法更改 UILabel 文本颜色

标签 objective-c uilabel uicolor

我想更改 UILabel 文本颜色但我无法更改颜色,这就是我的代码的样子。

UILabel *categoryTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 46, 16)];
categoryTitle.text = @"abc";
categoryTitle.backgroundColor = [UIColor clearColor];
categoryTitle.font = [UIFont systemFontOfSize:12];
categoryTitle.textAlignment = UITextAlignmentCenter;
categoryTitle.adjustsFontSizeToFitWidth = YES;
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];
[self.view addSubview:categoryTitle];
[categoryTitle release];

标签文本颜色是白色,不是我的自定义颜色。

感谢您的帮助。

最佳答案

UIColor 的 RGB 分量在 0 和 1 之间缩放,最高不超过 255。

试试

categoryTitle.textColor = [UIColor colorWithRed:(188/255.f) green:... blue:... alpha:1.0];

在 swift 中:

categoryTitle.textColor = UIColor(red: 188/255.0, green: ..., blue: ..., alpha: 1)

关于objective-c - 无法更改 UILabel 文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2532409/

相关文章:

ios - 检查 iOS 模拟器类型和版本

ios - 应用崩溃 +[NSDecimalNumber gad_negativeOne] : issue?

ios - NSOperation completionBlock 被调用了两次

ios - 如何将 UILabel 居中对齐在 UIImageView 顶部

objective-c - 如何正确从 UIColor 获取亮度?

iOS - 更改 UISwitch 按钮颜色并调整其大小

ios - 添加多个 UIWindow

ios - 当来自 NotificationCenter 时,为什么调整标签大小需要延迟才能按预期更新?

ios - 具有给定字体的 2 行 UILabel 的高度

swift - 将十六进制颜色作为字符串转换为Uint