moonvader
moonvader

Reputation: 21111

Make selected button titlelabel to be uppercase

I have button with title "Button". After some actions this button gets "selected" status. While it is selected it have different text color that is easy to specify in interface builder.

The problem is that I also want set it's title to be uppercase. Which I don't know how.

Upvotes: 0

Views: 6239

Answers (3)

Ivan Nesterenko
Ivan Nesterenko

Reputation: 969

If you are setting it from code use

var myString = "string"
myString = myString.uppercaseString
button.setTitle(myString, forState: .Selected)

Upvotes: 0

Badal Shah
Badal Shah

Reputation: 7612

There is a built in function for that

Button.capitalizedString

So you can use it in this way in swift

Let me assume that abtn is your IBOutlet of UIButton and Button is titlelabel of UIButton.

 abtn.setTitle(abtn.titleLabel!.text.uppercaseString, forState: .Selected)

as said by kirsteins

To capitalize only the first letter you can use:

nameOfString.replaceRange(nameOfString.startIndex...nameOfString.startIndex, with: String(nameOfString[nameOfString.startIndex]).capitalizedString)

Upvotes: 0

technerd
technerd

Reputation: 14514

You can set Title for Selected State of UIButton from Attribute Inspector. Please find below image :

enter image description here

You can also set it programatically.

Objective-C

[btn setTitle:btn.titleLabel.text.uppercaseString forState:UIControlStateSelected];

Swift

 btn.setTitle(btn.titleLabel!.text.uppercaseString, forState: .Selected)

Upvotes: 6

Related Questions