ios 如何设置UIButton的标题文字颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31088172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the title text color of UIButton?
提问by coolmac
I tried changing the colors of the text for a button, but it's still staying white.
我尝试更改按钮文本的颜色,但它仍然保持白色。
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.
我也尝试将其更改为红色、黑色、蓝色,但没有任何反应。
回答by luk2302
回答by Vyacheslav
Swift UIsolution
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)
回答by handiansom
Example in setting button title color
设置按钮标题颜色的示例
btnDone.setTitleColor(.black, for: .normal)
回答by Jay Mayu
This is swift 5 compatible answer. If you want to use one of the built-in colours then you can simply use
这是 swift 5 兼容的答案。如果你想使用其中一种内置颜色,那么你可以简单地使用
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 颜色代码的自定义颜色,并在整个应用程序中重复使用。
回答by Ramprasath Selvam
func setTitleColor(_ color: UIColor?, for state: UIControl.State)
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.
颜色:
用于指定状态的标题颜色。state:
使用指定颜色的状态。UIControl.State 中描述了可能的值。
Sample:
样品:
let MyButton = UIButton()
MyButton.setTitle("Click Me..!", for: .normal)
MyButton.setTitleColor(.green, for: .normal)
回答by Shy Attoun
referring to radio buttons ,you can also do it with Segmented Control as following:
参考单选按钮,您也可以使用分段控制来执行以下操作:
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)
}
}