ios 如何更改 UIButton 标题颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2474289/
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 can I change UIButton title color?
提问by HelloWorld
I create a button programmatically..........
我以编程方式创建了一个按钮.......
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
how can I change title color?
如何更改标题颜色?
回答by Ben Gottlieb
You can use -[UIButton setTitleColor:forState:]
to do this.
您可以使用它-[UIButton setTitleColor:forState:]
来执行此操作。
Example:
例子:
Objective-C
目标-C
[buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Swift 2
斯威夫特 2
buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)
Swift 3
斯威夫特 3
buttonName.setTitleColor(UIColor.white, for: .normal)
Thanks to richardchildan
感谢理查柴尔德
回答by Anbu.Karthik
You created the UIButton
is added the ViewController
, The following instance method to change UIFont
, tintColor
and TextColor
of the UIButton
您创建的UIButton
添加了ViewController
,下面的实例方法的变化UIFont
,tintColor
和TextColor
的UIButton
Objective-C
目标-C
buttonName.titleLabel.font = [UIFont fontWithName:@"LuzSans-Book" size:15];
buttonName.tintColor = [UIColor purpleColor];
[buttonName setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
Swift
迅速
buttonName.titleLabel.font = UIFont(name: "LuzSans-Book", size: 15)
buttonName.tintColor = UIColor.purpleColor()
buttonName.setTitleColor(UIColor.purpleColor(), forState: .Normal)
Swift3
斯威夫特3
buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15)
buttonName.tintColor = UIColor.purple
buttonName.setTitleColor(UIColor.purple, for: .normal)
回答by Rupom
Solutionin Swift 3:
解决方案在斯威夫特3:
button.setTitleColor(UIColor.red, for: .normal)
This will set the title color of button.
这将设置按钮的标题颜色。
回答by Imanou Petit
With Swift 5, UIButton
has a setTitleColor(_:for:)
method. setTitleColor(_:for:)
has the following declaration:
有了 Swift 5,UIButton
就有了setTitleColor(_:for:)
方法。setTitleColor(_:for:)
有以下声明:
Sets the color of the title to use for the specified state.
设置用于指定状态的标题颜色。
func setTitleColor(_ color: UIColor?, for state: UIControlState)
The following Playground sample code show how to create a UIbutton
in a UIViewController
and change it's title color using setTitleColor(_:for:)
:
下面的游乐场代码示例展示了如何创建UIbutton
一个UIViewController
使用改变它的标题颜色setTitleColor(_:for:)
:
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
// Create button
let button = UIButton(type: UIButton.ButtonType.system)
// Set button's attributes
button.setTitle("Print 0", for: UIControl.State.normal)
button.setTitleColor(UIColor.orange, for: UIControl.State.normal)
// Set button's frame
button.frame.origin = CGPoint(x: 100, y: 100)
button.sizeToFit()
// Add action to button
button.addTarget(self, action: #selector(printZero(_:)), for: UIControl.Event.touchUpInside)
// Add button to subView
view.addSubview(button)
}
@objc func printZero(_ sender: UIButton) {
print("0")
}
}
let controller = ViewController()
PlaygroundPage.current.liveView = controller
回答by fagiani
If you are using Swift, this will do the same:
如果您使用 Swift,这将执行相同的操作:
buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)
buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)
Hope that helps!
希望有帮助!