为不的UILabel设置

为不的UILabel设置

问题描述:

我创建的UIButton这样的文字颜色:为不的UILabel设置

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

+0

使用setTitleColor – koropok

+3

的可能的复制[如何设置UIButton的标题文本颜色?(https://*.com/questions/31088172/how-to-set-the-title-text-color -of-的UIButton) – Yuyutsu

使用title color

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

使用框架的要求&添加到您的视图 -

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) 
} 

首先设置框的大小。

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)