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);
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

7 Answers7

132

I think you want setTitle: forState:

[button setTitle:@"Baha'i" forState:UIControlStateNormal]
Pang
  • 9,564
  • 146
  • 81
  • 122
Tobias Cohen
  • 19,893
  • 7
  • 54
  • 51
  • 1
    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. – David Potter Feb 20 '12 at 23:27
  • 2
    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? – Gmu Sep 20 '13 at 04:07
  • 2
    @Gmu They've found it very hard to implement at apple – LolaRun Sep 03 '14 at 22:18
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];
ddiego
  • 3,247
  • 1
  • 28
  • 18
  • 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. – Fabian May 13 '13 at 21:08
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.

Faizan Refai
  • 833
  • 10
  • 13
2

For swift Developers-

button.setTitle("Your button Name", for: .normal)
iDeveloper
  • 2,339
  • 2
  • 24
  • 38
2

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

btnLogin.setTitle("Welcome, " + var_name, for: [])
Will Buffington
  • 1,048
  • 11
  • 13
0
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
  • 2
    Isn’t this essentially the same as [@iDeveloper’s answer from three years ago](https://stackoverflow.com/a/39932067/3025856)? – Jeremy Caney Mar 01 '21 at 02:55
-4
  self.Button_name.titleLabel.text = @"Your title name";
Dhara
  • 4,093
  • 2
  • 36
  • 69