【问题标题】:Text color for uilabel not settinguilabel 的文本颜色未设置
【发布时间】:2017-08-14 07:04:13
【问题描述】:

我这样创建 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)

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

【问题讨论】:

标签: swift uibutton


【解决方案1】:

使用title color

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      先设置帧大小。

          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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-17
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        相关资源
        最近更新 更多