user426795
user426795

Reputation: 11683

How to hide a UIButton title after onclick event

When I click a button Its title should be hide. I do't want to set the title to empty string @"". So, How can i do this?

Upvotes: 1

Views: 5556

Answers (9)

user2871466
user2871466

Reputation:

// Hide text
button.titleLabel.layer.opacity = 0.0f;

// Show text
button.titleLabel.layer.opacity = 1.0f;

Upvotes: 1

apouche
apouche

Reputation: 9983

using button.titleLabel.hidden = YES will not work (at least on on iOS 7).

I ended up using:

// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];

Upvotes: 1

Claudio
Claudio

Reputation: 2037

Just a better solution that I'm using now:

The accepted answer doesn't let me re-use the name if I need, so I'm using in this way:

-(void)SomeButtonPressed {
    someButton.titleLabel.textColor = [UIColor clearColor];
}

I think it's better just keeping the button's label invisible.

Upvotes: 4

Tim Sullivan
Tim Sullivan

Reputation: 16888

Why don't you want to set the title to an empty string? Simply store the value in a local field and set the button's title to @"" and everything will be fine.

In your .h:

NSString *someLocalField;

in your .m:

-(void)SomeButtonPressed {
  someLocalField = someButton.text;
  someButton.text = @"";
}

This way, if you ever need to restore the text of the button, you can do so:

someButton.text = someLocalField

If you want to do this for a bunch of buttons, you could always use an NSDictionary and associate the string values with the buttons.

Upvotes: 2

eviltrue
eviltrue

Reputation: 679

Have you tried button.titleLabel.hidden = YES ?

Upvotes: 0

rano
rano

Reputation: 5676

I guess here you can find a solution, simply make the label that contains the text hidden.

button.titleLabel.hidden = YES;

You can put this in an IBAction linked in InterfaceBuilder to the Touch Inside Up event associate to your button

Upvotes: 0

tc.
tc.

Reputation: 33602

If you want it to disappear when a finger is on the button,

[button setTitle:@" " forState:UIControlStateHighlighted];

If you want it to toggle between being displayed and not,

[button setTitle:@" " forState:UIControlStateSelected];
[button setTitle:@" " forState:UIControlStateSelected|UIControlStateHighlighted];

and then set button.selected = !button.selected in the button action.

I'm using a single space instead of the empty string because sometimes the empty string has special handling which makes it equivalent to nil. If the empty string works, you can use that instead.

Upvotes: 2

theMikeSwan
theMikeSwan

Reputation: 4729

Just hiding the title sounds a bit odd and not very Apple interface like. You can also just set the button to be hidden and then the entire thing goes away. If you do want the title to be the only thing that goes away (keep in mind that the button will still work in this state, just not have a title) then you could always assign the background color someButton.currentTitleColor = someButton.backgroundColor; should make the text vanish (you may need to set the shadow color as well).

Upvotes: 0

NWCoder
NWCoder

Reputation: 5266

I haven't tried this, but if you need the title text to stay the same, but still hide from a user you might be able to set the font color to [UIColor clearColor];

Upvotes: 1

Related Questions