uilabel没有设置的文字颜色

时间:2017-08-14 07:04:13

标签: swift uibutton

我像这样创建UIButton:

okBtn = UIButton()
okBtn.setTitle("OK", for: .normal)
okBtn.setTitle("OK", for: .selected)
okBtn.titleLabel?.textColor = .purpleLight
okBtn.backgroundColor = .red
okBtn.addTarget(self, action: #selector(didTapOKBtn), for: .touchUpInside)
self.addSubview(okBtn)

然而,没有设置颜色,我添加了一个屏幕来显示它的外观。

enter image description here

3 个答案:

答案 0 :(得分:2)

Use title color

okBtn.setTitleColor(UIColor.blue, for: UIControlState.normal)

答案 1 :(得分:1)

根据需要使用框架&添加到您的视图 -

   override func viewDidLoad() {
    super.viewDidLoad()
    let okBtn = UIButton(frame: CGRect(x: 20, y: 70, width: 50, height: 50))
    okBtn.setTitle("OK", for: .normal)
    okBtn.backgroundColor = .red
    okBtn.setTitleColor(.white, for: .normal)
    view.addSubview(okBtn)
}

答案 2 :(得分:1)

首先设置帧大小。

    okBtn = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) //Sets the frame size on your viewController
    okBtn.setTitle("OK", for: .normal)
    okBtn.setTitleColor(UIColor.purple, for: .normal) //Sets the color of the text on the button
    //There is no color as purpleLight. You need to set the rgb to get your desired color.
    okBtn.backgroundColor = .red
    okBtn.addTarget(self, action: #selector(didTapOKBtn), for: .touchUpInside)
    self.view.addSubview(okBtn)