iPhone/iPad UIButton TitleLabel text not appearing

后端 未结 6 1479
北海茫月
北海茫月 2021-02-05 01:08

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 fi

相关标签:
6条回答
  • 2021-02-05 01:52

    For swift Developers-

    button.setTitle("Your button Name", for: .normal)
    
    0 讨论(0)
  • 2021-02-05 01:53

    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];
    
    0 讨论(0)
  • 2021-02-05 02:00

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

    btnLogin.setTitle("Welcome, " + var_name, for: [])
    
    0 讨论(0)
  • 2021-02-05 02:01
      self.Button_name.titleLabel.text = @"Your title name";
    
    0 讨论(0)
  • 2021-02-05 02:04
    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.

    0 讨论(0)
  • 2021-02-05 02:11

    I think you want setTitle: forState:

    [button setTitle:@"Baha'i" forState:UIControlStateNormal]
    
    0 讨论(0)
提交回复
热议问题