【问题标题】:How to set the title text color of UIButton?如何设置 UIButton 的标题文字颜色?
【发布时间】:2015-09-14 07:01:25
【问题描述】:

我尝试更改按钮文本的颜色,但它仍然保持白色。

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)

我也尝试将其更改为红色、黑色、蓝色,但没有任何反应。

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    如果你使用的是没有 NSAttribute 的按钮,那么你可以简单地使用设置按钮的标题和颜色

    yourButtonName.setTitle("Your Title", for: .normal)
    
    enterCustomAmount.setTitleColor(.gray, for: .normal)
    

    但是如果你在你的按钮属性中提供 NSAttribute 那么你首先需要为按钮设置属性

    var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9")]
    

    或者对于多个属性,您可以使用逗号分隔符,例如

    var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9"), NSAttributedString.Key.font: UIFont(name: "Dubai-Medium", size: 16) ]
    

    并像这样将其分配给您的 UIButton

     let myString = "Your title"
                let text = NSAttributedString(string: myString, attributes: myAttribute)
                enterCustomAmount.setAttributedTitle(text, for: .normal)
    

    【讨论】:

      【解决方案2】:

      您可以使用十六进制代码设置 UIButton 标题颜色

      btn.setTitleColor(UIColor(hexString: "#95469F"), for: .normal)
      

      【讨论】:

        【解决方案3】:

        设置标题颜色

        btnGere.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        

        【讨论】:

          【解决方案4】:

          您必须像设置实际标题文本一样使用func setTitleColor(_ color: UIColor?, for state: UIControlState)Docs

          isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), for: .normal)
          

          【讨论】:

          • 为了可读性,我不想限定我的枚举,但这只是我
          • @styler1972 我也更喜欢这个——显然我最初写答案时并没有意识到这一点
          • isbeauty.setTitleColor(UIColorFromRGB(rgbValue: "F21B3F"), for: .Normal)
          • 我相信从 xcode 9 开始,它现在是“.normal”而不是“.Normal”
          • 我的代码:yourButtonName.setTitleColor(UIColor.red, for: .normal)
          【解决方案5】:

          参考单选按钮,您也可以使用分段控件进行如下操作:

          第 1 步: 将分段控件拖到您的视图中 在属性检查器中更改两个段的标题,例如“男性”和“女性”

          第 2 步: 在代码中为它创建一个出口和一个动作

          第 3 步: 创建一个变量以供将来使用以包含选择的数据

          在代码中做如下:

          @IBOutlet weak var genderSeg: UISegmentedControl!
          
          var genderPick : String = ""
          
          
           @IBAction func segAction(_ sender: Any) {
          
              if genderSeg.selectedSegmentIndex == 0 {
                   genderPick = "Male"
                  print(genderPick)
              } else if genderSeg.selectedSegmentIndex == 1 {
                  genderPick = "Female"
                    print(genderPick)
              }
          }
          

          【讨论】:

            【解决方案6】:

            Swift UI解决方案

            Button(action: {}) {
                        Text("Button")
                    }.foregroundColor(Color(red: 1.0, green: 0.0, blue: 0.0))
            

            斯威夫特 3、斯威夫特 4、斯威夫特 5

            改进 cmets。 这应该有效:

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

            【讨论】:

              【解决方案7】:
              func setTitleColor(_ color: UIColor?, 
                             for state: UIControl.State)
              

              参数

              颜色:
              用于指定状态的标题颜色。

              状态:
              使用指定颜色的状态。 UIControl.State 中描述了可能的值。

              示例

              let MyButton = UIButton()
              MyButton.setTitle("Click Me..!", for: .normal)
              MyButton.setTitleColor(.green, for: .normal)
              

              【讨论】:

                【解决方案8】:

                这是 swift 5 兼容的答案。如果你想使用其中一种内置颜色,那么你可以简单地使用

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

                如果您想要一些自定义颜色,请先为 UIColor 创建一个扩展,如下所示。

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

                然后将其用于您的按钮,如下所示。

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

                提示:您可以使用此方法存储来自 rgba 颜色代码的自定义颜色,并在整个应用程序中重复使用它。

                【讨论】:

                  【解决方案9】:

                  按钮标题颜色设置示例

                  btnDone.setTitleColor(.black, for: .normal)
                  

                  【讨论】:

                  • 我的答案和你的有什么区别?
                  • 很明显 - 颜色
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-10-05
                  • 1970-01-01
                  • 2011-01-29
                  • 2015-07-07
                  • 1970-01-01
                  • 2010-10-10
                  • 1970-01-01
                  相关资源
                  最近更新 更多