iOS: UIButton titleLabel -- does it do anything at all?

40,121

Solution 1

Setting the titleLabel's text property like that has no effect. Instead, call -setTitle:forState: on the button:

[editButton setTitle:@"Edit" forState:UIControlStateNormal]

The reason for this is because the button can have different titles for different states (e.g., UIControlStateDisabled, UIControlStateHighlighted). Setting a property for the UIControlStateNormal control state will apply to all the states if you don't specify the others explicitly.

Per the documentation for UIButton:

This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.

You can customize label's color and shadow color based on the state. See -setTitleColor:forState and -setTitleShadowColor:forState, respectively. The rest of the properties on titleLabel, such as textAlignment and font, should work as you have them set now and should apply to all the control states.

Specifically, see the documentation for UIButton's titleLabel property: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel

titleLabel itself is read-only, but that doesn't mean you can't change the values of its own properties, such as font, line break mode, etc.

Solution 2

Here's how I worked it out using Swift 4.2.

counterButton.setTitle("Start Counter",
                      for:UIControl.State.normal)
Share:
40,121

Related videos on Youtube

Amagrammer
Author by

Amagrammer

Long time programmer with unhealthy fascinations for algorithm optimization and chocolate. I've developed for VMS, UNIX, Windows, Game Boy, Playstation, (Windows again), embedded systems, (ugh, Windows) and iPhone, in more or less that order. Lead programmer for "Urban Strike", the first Game Gear product ever to pass SEGA QA on the first try. When I am not hunched over a keyboard, I read fiction, host a chapter of Drinking Liberally, and dream of retirement to a sprawling country estate.

Updated on July 09, 2022

Comments

  • Amagrammer
    Amagrammer almost 2 years

    I want to make a UIButton with type UIButtonTypeCustom (for the shape). I want to assign the title using button.titleLabel because I need to specify the font. The following code looks like it should work, but doesn't -- no label shows up, period.

    UIImage *editButtonImage = [UIImage imageNamed: @"editButton.png"];
    float width = editButtonImage.size.width;
    float height = editButtonImage.size.height;
    
    UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
    editButton.frame = CGRectMake(0, 0, width, height);
    [editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
    editButton.adjustsImageWhenHighlighted = YES;
    editButton.titleLabel.text = @"Edit";
    editButton.titleLabel.textColor = [UIColor whiteColor];
    editButton.titleLabel.textAlignment = UITextAlignmentCenter;
    editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
    [self.view addSubview: editButton];
    

    Everyone always says to use setTitle:forState: but that gives you a font I don't like. The titleLabel method is NOT deprecated -- it should work.

    I have run into this several times before and always just worked around it, but I'd really like to figure it out. Any ideas?

  • Amagrammer
    Amagrammer over 13 years
    So the ONE thing you can't set using titleLabel is the actual text value. That seems a bit strange to me, but okay.
  • CIFilter
    CIFilter over 13 years
    It is a bit counterintuitive at first. Seemingly, setting the label's text property would just be a shortcut for calling -setTitle:forState, but it isn't.
  • arlomedia
    arlomedia almost 12 years
    Similarly it looks like you can't set the textColor property of titleLabel directly, but have to use the setTitleColor:forState: method. Thanks for mentioning this, because it's not in the documentation.
  • Phil Andrews
    Phil Andrews over 8 years
    titleLabel.textAlignment is incorrect. To align the text you must use titleLabel.contentHorizontalAlignment = .Left