[英]How to set the title text color of UIButton?
I tried changing the colors of the text for a button, but it's still staying white.我尝试更改按钮文本的 colors,但它仍然保持白色。
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)
I also tried changing it the red, black, blue, but nothing is happening.我也尝试将其更改为红色、黑色、蓝色,但没有任何反应。
Swift UI solution Swift UI解决方案
Button(action: {}) {
Text("Button")
}.foregroundColor(Color(red: 1.0, green: 0.0, blue: 0.0))
Swift 3, Swift 4, Swift 5斯威夫特 3、斯威夫特 4、斯威夫特 5
to improve comments.以改进评论。 This should work:
这应该有效:
button.setTitleColor(.red, for: .normal)
设置按钮标题颜色的示例
btnDone.setTitleColor(.black, for: .normal)
This is swift 5 compatible answer.这是 swift 5 兼容的答案。 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.如果你想要一些自定义颜色,那么首先为 UIColor 创建一个扩展,如下所示。
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.提示:您可以使用此方法存储来自 rgba 颜色代码的自定义颜色,并在整个应用程序中重复使用。
func setTitleColor(_ color: UIColor?, for state: UIControl.State)
Parameters :参数:
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.
UIControl.State 中描述了可能的值。
Sample :样品:
let MyButton = UIButton()
MyButton.setTitle("Click Me..!", for: .normal)
MyButton.setTitleColor(.green, for: .normal)
referring to radio buttons ,you can also do it with Segmented Control as following:参考单选按钮,您也可以使用 Segmented Control 执行以下操作:
step 1: drag a segmented control to your view in the attribute inspector change the title of the two segments ,for example "Male" and "Female"第 1 步:在属性检查器中将分段控件拖到您的视图中,更改两个分段的标题,例如“男”和“女”
step 2: create an outlet & an action for it in the code第 2 步:在代码中为它创建一个插座和一个动作
step 3: create a variable for future use to contain choice's data第 3 步:创建一个变量以供将来使用以包含选择的数据
in the code do as following:在代码中执行以下操作:
@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)
if you are using a button without NSAttribute then you can simply set the title and color of button by using如果您使用的是没有 NSAttribute 的按钮,那么您可以简单地通过使用设置按钮的标题和颜色
yourButtonName.setTitle("Your Title", for: .normal)
enterCustomAmount.setTitleColor(.gray, for: .normal)
but if you give NSAttribute in your button property then you first need to set the attribute for the button但是如果你在你的按钮属性中给出 NSAttribute 那么你首先需要为按钮设置属性
var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9")]
or for more then one property you can use comma separator like或者对于多个属性,您可以使用逗号分隔符,例如
var myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.init(hexString: "#FFAEA9"), NSAttributedString.Key.font: UIFont(name: "Dubai-Medium", size: 16) ]
and assign it to your UIButton like this并像这样将其分配给您的 UIButton
let myString = "Your title"
let text = NSAttributedString(string: myString, attributes: myAttribute)
enterCustomAmount.setAttributedTitle(text, for: .normal)
To change color of text更改文本颜色
button.titleLabel.textColor = UIColor.grayColor()
To change state, on button press add following -要更改 state,请在按下按钮时添加以下内容 -
button.enabled = true
IBAction method should be like - IBAction 方法应该像 -
@IBAction func buttonTapped(sender : UIButton!) {
sender.enabled = false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.