iPhone/iPad UIButton TitleLabel text not appearing
Asked Answered
I

7

36

I created a grid of buttons. The following code creates the buttons and displays them, but there is no text on the button. Is there a setting I am missing? (Obj-C replies are fine, I'm bi-lingual)

RectangleF frame = new RectangleF (X + 3, Y + 3, cellWidth - 2, cellHeight - 2);
UIButton button = new UIButton (frame);
button.TitleLabel.Text = "Baha'i";
button.TitleLabel.Font = UIFont.FromName ("Helvetica-Bold", 15);
button.TitleLabel.TextColor = UIColor.Black;
button.TitleLabel.Frame = frame;
button.BackgroundColor = UIColor.FromWhiteAlpha(.5f,.5f);
this.AddSubview (button);
Imine answered 7/3, 2010 at 2:33 Comment(2)
Why were you using capital letters for titleLabel? It's button.titleLabel, not button.**T**itleLabel.Ergot
I've detailed an explanation here https://mcmap.net/q/204524/-changing-uibutton-textMeghannmegiddo
B
132

I think you want setTitle: forState:

[button setTitle:@"Baha'i" forState:UIControlStateNormal]
Brantley answered 7/3, 2010 at 2:41 Comment(3)
What's interesting is that button.titleLable.text = @"Foo" doesn't always do the right thing. You have to set the title on the UIButton itself.Babel
this is ridiculous button.titleLabel.text does no work but [button setTile:.. forState:] works wish it is set by default for normal state or something. Comments?Cahier
@Cahier They've found it very hard to implement at appleUne
S
6

A UIButton inherits from UIView, so another way to add text and images, is to add subview. Example:

// My Image
UIImage *buttonImage = [UIImage imageNamed:@"myImage.png"];
UIImageView *buttonImageView = [[[UIImageView alloc] 
    initWithFrame:CGRectMake(0, 0, 
    buttonImage.size.width,buttonImage.size.height)] autorelease];
[buttonImageView setImage:buttonImage];

// My Label
UILabel* titleLabel = [[[UILabel alloc] 
    initWithFrame:CGRectMake(0, 0, 
    buttonImage.size.width, buttonImage.size.height)] autorelease];
titleLabel.text = @"My Text";
titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 11.0];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;

// Create Button with Image and Label
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addSubview:buttonImageView];
[button addSubview:titleLabel];
Sigmund answered 5/5, 2011 at 19:14 Comment(1)
A UIButton already has a label. Adding your own as you show isn't usually necessary. @tobias-cohen has the right answer for this question, and most cases.Quaver
A
3
UIButton *  selectCountryBtn =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];  

[selectCountryBtn setTitle:@"My Title" forState:UIControlStateNormal];

Here, I have create sample programmatically button, and then set the title for its Normal Control state, You can use your own button object for set the title string, additionally you can also set title text for another control states by change UIControlStateNormal to another required states.

Ahn answered 2/3, 2012 at 7:30 Comment(0)
F
2

For swift Developers-

button.setTitle("Your button Name", for: .normal)
Fascine answered 8/10, 2016 at 11:59 Comment(0)
F
2

In my case (Swift 4) it was the following:

btnLogin.setTitle("Welcome, " + var_name, for: [])
Firmin answered 15/11, 2018 at 0:7 Comment(0)
T
0
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
Trombidiasis answered 28/2, 2021 at 13:57 Comment(1)
Isn’t this essentially the same as @iDeveloper’s answer from three years ago?Honk
M
-4
  self.Button_name.titleLabel.text = @"Your title name";
Minervamines answered 26/2, 2013 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.