How to set the title text color of UIButton?

后端 未结 7 1406
温柔的废话
温柔的废话 2020-12-13 05:18

I tried changing the colors of the text for a button, but it\'s still staying white.

isbeauty = UIButton()
isbeauty.setTitle(\"Buy\", forState: UIControlStat         


        
7条回答
  •  半阙折子戏
    2020-12-13 05:55

    This is swift 5 compatible answer. If you want to use one of the built-in colours then you can simply use

    button.setTitleColor(.red, for: .normal)
    

    If you want some custom colours, then create an extension for a UIColor as below first.

    import UIKit
    extension UIColor {
        static var themeMoreButton = UIColor.init(red: 53/255, green: 150/255, blue: 36/255, alpha: 1)
    }
    

    Then use it for your button as below.

    button.setTitleColor(UIColor.themeMoreButton, for: .normal)
    

    Tip: You can use this method to store custom colours from rgba colour code and reuse it throughout your application.

提交回复
热议问题