Make selected button titlelabel to be uppercase - ios

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.

You can set Title for Selected State of UIButton from Attribute Inspector.
Please find below image :
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)

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)

If you are setting it from code use
var myString = "string"
myString = myString.uppercaseString
button.setTitle(myString, forState: .Selected)

Related

Is it possible for a uibutton to set the title of another uibutton seeing that both were created in the storyboard?

I have just started coding not too long ago, so apologies in advance for any glaringly obvious mistakes.
I have created both buttons using the storyboard. I understand that you can change the sender button's title like how this solution stated it, but I was still not sure how to change the other button title when the first one is clicked. I have tried referencing tags, but nothing happened when I clicked the first button. Is it possible to do this? Please give an example if it is.
Goal: Change the title of one button by clicking another button, both created with Storyboard.
There are two solution to achieve this:
By using IBOutlets
By using Storyboard tags
Using IBOutlets, you can create an outlet of second button e.g.
#IBOutlet weak var btnSecond: UIButton!
Now change the title of this button in action of first button like this:
#IBAction clickFirstBtn(sender: UIButton){
btnSecond.set("New Title", for: .normal)
}
Using storyboard tags, you can give any tag to second button except 0. For example let the tag of second button = 10.
Now you change the title like this:
#IBAction clickFirstBtn(sender: UIButton){
if let btnSecond = view.viewWithTag(10) as? UIButton{
btnSecond.set("New Title", for: .normal)
}
}
(I'll answer to this question even if there already are correct answers because they both are showing swift solutions even if the question is expicitly about Objective C)
The simplest way, as stated in this other answer, is to assign outlets to the buttons. Ctrl-drag from the button in Interface Builder to the #interface of your ViewController and give the button an outlet name:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *button2;
#end
Then, in the first button's IBAction, call the setTitle:forControlState: method of the second button outlet
- (IBAction)button1Tapped:(id)sender {
[self.button2 setTitle:#"new title" forState:UIControlStateNormal];
}
As an alternative, you can also use storyboard tags (the fact that i used a UIView and then checked if isKindOfClass is to avoid crashes in case you mistakenly assign the same tag to some non-UIButton object):
- (IBAction)button1Tapped:(id)sender {
UIView *buttonView = [self.view viewWithTag:1002];
if ([buttonView isKindOfClass:[UIButton class] ]) {
[(UIButton *)buttonView setTitle:#"new title" forState:UIControlStateNormal];
}
}
Lets say you have Button1 and Button2 and you want to set the title of Button2 onclick of Button1.
You can do it by creating an action on the click of Button1 and setting the title of Button2 on this action..
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
Button1.addTarget(self, action: #selector(setTitle), for: .touchUpInside)
}
#objc func setTitle()
{
Button2.setTitle("Button Title",for: UIControlState.Normal)
}

How to change title of UILabel when they create themselve in Swift?

I override draw func in UIButton class and i need to set unique label for this button when he create themselve.
When i use super.titleLabel?.text = "But Me" - this is working, but before first click on button. After click, title change to standart "Button". Please help.
P.S.: title label must be set up into UIButton class, not into ViewController.
You need to set button title as below :
button.setTitle("Button Title",for: .normal)

Make UIButton both image and text tappable

I have a UIButton with text and an image that looks like this:
But when I click the button it goes like this (just the image is selected instead of both the image and the text, I want button text also be selected.
How can I fix it?
Yes, you can set this color in your xib (interface builder) also, just select your button and inside State config select Highlighted and then set the textColor for that state. Check image for reference.
Objective-C
Use [UIButton setTitleColor:forState:] API
[yourButton setTitleColor:[UIColor grayColor] forState: UIControlStateHighlighted];
Swift
yourButton.setTitleColor(UIColor.grayColor(), forState: .Highlighted)

Swift: remove background color when button clicked

I created a button and when it is clicked, it's background image changes. However the issue is when I click it , it gives a rectangle with a color.
Image:
Should Be:
I don't want to see that clicking background.
My codes are below:
let on = UIImage(named: "on") as UIImage
let off = UIImage(named: "off") as UIImage
btn.setBackgroundImage(off, forState: .Normal)
When you use Storyboard buttons which is IBOutlet UIButton object. By default it is not set as a custom type. So if you set background of this button as an image, It shows border of that button with the image.
For Storyboard variables:
Select UIButton control in Storyboard. Go to Attribute Inspector > Change type System to Custom
See the reference images
For programmatically created buttons:
If you create UIButton object programmatically than set button type as UIButtonTypeCustom.
Objective C Code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
Swift Code:
var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
just use this
btn.setBackgroundImage(nil, forState: .Normal)

is it possible to update UIButton title/text programmatically?

I have a UIButton, that when pressed, brings up a new view where the user can change some settings. When the view is dismissed, I'd like to update the title/text of the UIButton to reflect the new state. I'm calling:
[myButton setTitle: #"myTitle" forState: UIControlStateNormal];
[myButton setTitle: #"myTitle" forState: UIControlStateApplication];
[myButton setTitle: #"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: #"myTitle" forState: UIControlStateReserved];
[myButton setTitle: #"myTitle" forState: UIControlStateSelected];
[myButton setTitle: #"myTitle" forState: UIControlStateDisabled];
But it never seems to change from the original text/title as specified in IB.
I solved the problem just setting the title parameter for UIControlStateNormal, and it automatically works on the other states. The problem seems to be when you set another UIControlState.
[myButton setTitle: #"myTitle" forState: UIControlStateNormal];
Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms.
Edit: While it's not the case here, something like this can also happen if you set an attributed title to the button, then you try to change the title and not the attributed title.
As of Swift 4:
button.setTitle("Click", for: .normal)
I discovered another problem. It may be a bug introduced in iOS 5, but I thought I'd point it out for anyone else who encounters it.
If you don't set any default text for the button in the XIB, no text will ever appear if you set it programmatically. And if you do set text in the XIB, any text you subsequently assign to the button programmatically will be truncated to the size of the default text.
And finally, if you're showing the view with your button and then invoke another view (like an ActionSheet) and then dismiss it, the text that you assigned to the button programmatically will be erased and the button caption will return to whatever you set up in the XIB.
Even though Caffeine Coma's issue was resolved, I would like to offer another potential cause for the title not showing up on a UIButton.
If you set an image for the UIButton using
- (void)setImage:(UIImage *)image forState:(UIControlState)state
It can cover the title. I found this out the hard way and imagine some of you end up reading this page for the same reason.
Use this method instead
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
for the button image and the title will not be affected.
I tested this with programmatically created buttons and buttons created in a .xib
for swift :
button.setTitle("Swift", forState: UIControlState.Normal)
One more possible cause is this:
If you attempt to set the button's title in the (id)initWithNibName: ... method, then you're button property will still be nil. It hasn't yet been assigned to the UIButton.
You must be sure that you're setting your buttons in a method like (void)viewWillLoad or (void)viewWillAppear, but you probably don't want to set them as late as (void)viewDidAppear.
Turns out the docs tell you the answer! The UIButton will ignore the title change if it already has an Attributed String to use (with seems to be the default you get when using Xcode interface builder).
I used the following:
[self.loginButton
setAttributedTitle:[[NSAttributedString alloc] initWithString:#"Error !!!" attributes:nil]
forState:UIControlStateDisabled];
[self.loginButton setEnabled:NO];
Sometimes it can get really complicated. The easy way is to "refresh" the button view!
//Do stuff to your button here. For example:
[mybutton setEnabled:YES];
//Refresh it to new state.
[mybutton setNeedsDisplay];
I kept having problems with this, the only solution was to add an image and label as subviews to the uibutton. Then I discovered that the main problem was that I was using a UIButton with title: Attributed. When I changed it to Plain, just setting the titleLabel.text did the trick!
#funroll is absolutely right. Here you can see what you will need Make sure function runs on main thread only. If you do not want deal with threads you can do like this for example: create NSUserDefaults and in ViewDidLoad cheking condition was pressed button in another View or not (in another View set in NSUserDefaults needed information) and depending on the conditions set needed title for your UIButton, so [yourButton setTitle: #"Title" forState: UIControlStateNormal];
Make sure you're on the main thread.
If not, it will still save the button text. It will be there when you inspect the object in the debugger. But it won't actually update the view.

Resources