如何设置UIButton的标题文字颜色?

How to set the title text color of UIButton?

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

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)

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

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

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

SwiftUI解法

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

Swift3,Swift4,Swift5

改进评论。 这应该有效:

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

设置按钮标题颜色示例

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

这是 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 颜色代码的自定义颜色,并在整个应用程序中重复使用它。

func setTitleColor(_ color: UIColor?, 
               for state: UIControl.State)

参数:

color:
The color of the title to use for the specified state.

state:
The state that uses the specified color. The possible values are described in UIControl.State.

示例:

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

参考单选按钮,您也可以使用分段控件来实现,如下所示:

第一步: 将分段控件拖到您的视图中 在属性检查器中更改两个段的标题,例如 "Male" 和 "Female"

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

设置标题颜色

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

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

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

如果您使用的是不带 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)