ios 更改 UIButton 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11417077/
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
Changing UIButton text
提问by Julian Coltea
So I'm trying to update the text on a UIButton when I click it. I'm using the following line to change the text:
因此,当我单击它时,我试图更新 UIButton 上的文本。我正在使用以下行来更改文本:
calibrationButton.titleLabel.text = @"Calibration";
I have verified that the text is changing, but when I run the app and I click on the button, it changes to "Calibration" for a split second and then goes right back to its default value. Any ideas why this might be happening? Is there some sort of refresh function I need to be calling?
我已经验证文本正在更改,但是当我运行应用程序并单击按钮时,它会在一瞬间更改为“校准”,然后立即恢复为默认值。任何想法为什么会发生这种情况?我需要调用某种刷新功能吗?
回答by Jesse Gumpo
When laying out its subviews, a UIButton will set its titleLabel's text value using its own title values, so that you can set up to four different strings for the four states (normal, highlighted, selected, disabled).
在布局其子视图时,UIButton 将使用自己的标题值设置其 titleLabel 的文本值,以便您可以为四种状态(正常、突出显示、选中、禁用)设置最多四个不同的字符串。
Because of this feature, setting the titleLabel's text directly won't persist, and will be reset by the button when it lays out its subviews.
因为这个特性,直接设置titleLabel的文本不会持久化,会在布局子视图时被按钮重置。
This is what you have to do to change the title text for a button's state.
这是更改按钮状态的标题文本所必须执行的操作。
[calibrationButton setTitle:@"Calibration" forState:UIControlStateNormal];
回答by Ashley Mills
To set button text use the following method:
要设置按钮文本,请使用以下方法:
[calibrationButton setTitle: @"Calibration" forState: UIControlStateNormal];
See UIButton
class reference for more details...
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
有关UIButton
更多详细信息,请参阅类参考...
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
Or in Swift 3:
或者在 Swift 3 中:
calibrationButton.setTitle("Calibration", for: .normal)
回答by bhautikmewada191
programmatically you can set button title like below:
您可以以编程方式设置按钮标题,如下所示:
[myButton setTitle:@"buttonTitle" forState:UIControlStateNormal];
you can also set button title property from storyboard.
您还可以从故事板设置按钮标题属性。
回答by user3429534
Not a huge deal, and possibly obvious, but there are several states available for buttons. If you provide the 'wrong' one, you will not see the text change as desired.
没什么大不了的,而且可能很明显,但是按钮有几种可用的状态。如果您提供了“错误”的内容,您将不会看到文本按需要更改。
I noticed that my button was not showing the text I added, using the methods shown here. Check this link to make sure you are providing the UIControlState that you intend.
我注意到我的按钮没有显示我添加的文本,使用这里显示的方法。检查此链接以确保您提供了您想要的 UIControlState。
What's the difference between UIControlStateHighlighted and UIControlStateSelected?
回答by Suraj Sonawane
For Swift 2.0:
对于 Swift 2.0:
let btnObject : UIButton = UIButton()
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
btnObject.setTitle("Button Title", forState: UIControlState.Normal)
btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)
回答by Stephen Chen
For Swift 3.0
对于 Swift 3.0
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
button.setTitle("set here", for: .normal)
button.addTarget(self, action: #selector(TableViewController.actionButtonTocuh), for: .touchUpInside)
button.titleLabel?.textColor = #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1)
view.addSubview(button)
回答by Johan Franzén
If you don't want to set the title for all states, just set it for the normal state since the title for the unset states will default to the title of the normal state.
如果您不想为所有状态设置标题,只需将其设置为正常状态,因为未设置状态的标题将默认为正常状态的标题。
btn.setTitle("Some text", for:.normal)