我试着为一个按钮改变文本的颜色,但它仍然是白色的。
isbeauty = UIButton()
isbeauty.setTitle("Buy", forState: UIControlState.Normal)
isbeauty.titleLabel?.textColor = UIColorFromRGB("F21B3F")
isbeauty.titleLabel!.font = UIFont(name: "AppleSDGothicNeo-Thin" , size: 25)
isbeauty.backgroundColor = UIColor.clearColor()
isbeauty.layer.cornerRadius = 5
isbeauty.layer.borderWidth = 1
isbeauty.layer.borderColor = UIColorFromRGB("F21B3F").CGColor
isbeauty.frame = CGRectMake(300, 134, 55, 26)
isbeauty.addTarget(self,action: "first:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(isbeauty)我也试着把它改成红色,黑色,蓝色,但什么都没发生。
发布于 2015-06-27 11:24:05
您必须以设置实际标题文本的方式使用func setTitleColor(_ color: UIColor?, for state: UIControl.State)。文档
isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), for: .normal)发布于 2017-01-25 14:18:58
Swift UI解决方案
Button(action: {}) {
            Text("Button")
        }.foregroundColor(Color(red: 1.0, green: 0.0, blue: 0.0))Swift 3,Swift 4,Swift 5
来改进评论。这应该是可行的:
button.setTitleColor(.red, for: .normal)发布于 2017-02-21 03:50:26
设置按钮标题颜色的示例
btnDone.setTitleColor(.black, for: .normal)https://stackoverflow.com/questions/31088172
复制相似问题