Peter
Peter

Reputation: 1878

UIButton titleLabel not displaying

I have seen a bunch of similar questions, but noone got the answer I am looking for. When I use this code to make a UIButton and set it's titleLabel, the UIButton appear, but the titleLabel won't appear.

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
button.titleLabel.text = @"Title";
button.titleLabel.font = [UIFont fontWithName:@"System-Bold" size:25.0f];
[self.view addSubview:button];

This code displays the button, but not the titleView, it's title. Why can that be? I am developing for iOS 6 and higher.

NOTE 1: I am not looking for an option like this:

[button setTitle:@"Title" forState:UIControlStateNormal];

Because I have to use the titleLabel to set it's font and fontSize.

NOTE 2: I can't create a UILabel and add it as a subView of the button. Since the button is later animating.

Upvotes: 36

Views: 22367

Answers (6)

Trev14
Trev14

Reputation: 4116

TLDR - try this saveButton.titleLabel?.layer.zPosition = 10

This was a frustrating one for me. I created a UIButton programmatically, added constraints, and added a gradient background (this is what caused my problem...I think). Then I needed to update the font:

saveButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
saveButton.setTitle("Save", for: .normal)

Notice I'm setting the title in the correct way. But still, no title showing. The craziest part - when I clicked "Debug View Hierarchy" I could see the title right there! If I didn't change the font, the title showed as well. Confusing. Then I added this (literally flinging spaghetti and seeing what stuck):

saveButton.titleLabel?.layer.zPosition = 10

It worked. It seems like some combination of gradient background, setting the font, etc was putting the button's internal title label at the bottom of its layers.

Upvotes: 0

vilas deshmukh
vilas deshmukh

Reputation: 399

If in the storyboard with a custom button image from assets like me

Check-in Attribute inspector

Asset image as image

Check-in Attribute inspector set asset image as a background

Asset image as back ground image

Upvotes: 2

Mahgolsadat Fathi
Mahgolsadat Fathi

Reputation: 3325

Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:

UIButton *button                  = [UIButton buttonWithType: UIButtonTypeSystem];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = NSLineBreakByTruncatingTail;

Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor(:for:) and setTitleShadowColor(:for:) methods of this class to make those changes. To set the actual text of the label, use setTitle(_:for:) (button.titleLabel.text does not let you set the text). The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.

in swift :

button.setTitle("your text", for: .normal)
button.setTitleColor(.white, for: .normal)

Upvotes: 1

iOSGuest
iOSGuest

Reputation: 61

Try using...

[button setNeedsLayout];
[button layoutIfNeeded];

It will force button to update the layout.

Upvotes: 3

mdziadkowiec
mdziadkowiec

Reputation: 1531

You always need to specify ControlState when updating button's title! There are four possible values for UIButtons:

UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected

so for example:

[button setTitle:@"Title" forState:UIControlStateNormal];

and then you can set custom settings for titleLabel:

[button.titleLabel setFont:[UIFont fontWithName:@"Zapfino" size:20.0]];
[button.titleLabel setTextColor:[UIColor blueColor]];

Upvotes: 83

andrew lattis
andrew lattis

Reputation: 1549

[button setTitle:@"Title" forState:UIControlStateNormal];

Is the correct way to set the title string.

titleLabel is used to set the font and color.

Upvotes: 10

Related Questions