Ios 如何更改UIButton标题颜色?

Ios 如何更改UIButton标题颜色?,ios,objective-c,iphone,uibutton,Ios,Objective C,Iphone,Uibutton,我以编程方式创建一个按钮 button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; [button setTitle:@"Show View" forState:UIControlStateNormal]; button.frame = CGRectM

我以编程方式创建一个按钮

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];

如何更改标题颜色?

您可以使用
-[UIButton setTitleColor:forState:://代码>来执行此操作

例如:

目标-C

[buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 buttonName.titleLabel.font = [UIFont fontWithName:@"LuzSans-Book" size:15];
 buttonName.tintColor = [UIColor purpleColor];
 [buttonName setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
Swift 2

buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)
Swift 3

buttonName.setTitleColor(UIColor.white, for: .normal)
buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15)
buttonName.tintColor = UIColor.purple
buttonName.setTitleColor(UIColor.purple, for: .normal)

由于

您创建了
ui按钮
并添加了
ViewController
,以下实例方法用于更改
ui按钮的
UIFont
tintColor
TextColor

目标-C

[buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 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

buttonName.setTitleColor(UIColor.white, for: .normal)
buttonName.titleLabel?.font = UIFont(name: "LuzSans-Book", size: 15)
buttonName.tintColor = UIColor.purple
buttonName.setTitleColor(UIColor.purple, for: .normal)

如果您使用的是Swift,这将执行相同的操作:

buttonName.setTitleColor(UIColor.blackColor(),forState:.Normal)


希望有帮助

对于Swift 5,
ui按钮
有一个方法
setTitleColor(:for:)
具有以下声明:

设置要用于指定状态的标题颜色


下面的示例代码显示了如何在
UIViewController
中创建
UIbutton
,并使用
setTitleColor(u:for:)
更改其标题颜色:


解决方案Swift 3中:

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

这将设置按钮的标题颜色。

例如[buttonName setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];我不敢相信[UIButton setTitleColor:forState:]能起作用,但btn.titleColor=[UIColor x]不能.@Lengus我不知道你如何访问
btn.titleColor
,只有
btn setTitleColor
可用的超级答案。你必须设置状态才能改变颜色,这太荒谬了…:(使用RGB如何?请不要简单地发布代码。请给出一些关于代码的解释、信息或用法。例如,请参阅。