当我使用自定义 UIColors 时标签的文本颜色没有改变

Text color for label not changing when I use custom UIColors

所以,当我这样做时,我的代码工作正常:

let label = UILabelBuiler("1", backgroundColor: UIColor.redColor(), textColor: UIColor.whiteColor(), font: UIFont(name: "FuturaStd-Heavy", size: 11)!)

但是当我这样做时它什么也没做:

let label = UILabelBuiler("1", backgroundColor: UIColor(red: 255, green: 75, blue: 75, alpha: 1), textColor: UIColor(red: 185, green: 200, blue: 202, alpha: 1), font: UIFont(name: "FuturaStd-Heavy", size: 11)!)

这是我使用第一个时的样子:

这是第二个(没有出现)

我的 UILabelBuilder 函数如下所示:

func UILabelBuiler(labelText: String, backgroundColor: UIColor, textColor: UIColor, font: UIFont) -> UILabel {
   let label = UILabel(frame: CGRectMake(218, 14, 15, 20))
   label.text = labelText
   label.numberOfLines = 0
   label.sizeToFit()
   label.backgroundColor = backgroundColor
   label.textColor = textColor
   label.font = font


   return label
}

此外,有谁知道我如何将标签的背景四舍五入?

创建颜色时,它期望的 rgba 值是 0...1 范围内的浮点数,因此您可以将颜色作为浮点数输入或除以 255

UIColor(red: 1.0, green: 0.29, blue: 0.29, alpha: 1.0)

UIColor(red: 255.0 / 255.0, green: 75.0 / 255.0, blue: 75.0 / 255.0, alpha: 1.0)

UIColor的颜色分量定义为

The {red, green, blue, alpha} component of the color object, specified as a value from 0.0 to 1.0.

相应地调整值或将每个值除以 255.0