Подтвердить что ты не робот

IPhone/iPad UIButton TitleLabel текст не отображается

Я создал сетку кнопок. Следующий код создает кнопки и отображает их, но на кнопке нет текста. Есть ли у меня настройки? (Ответы Obj-C в порядке, я двуязычный)

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);
4b9b3361

Ответ 1

Я думаю, вы хотите setTitle: forState:

[button setTitle:@"Baha'i" forState:UIControlStateNormal]

Ответ 2

UIButton наследует UIView, поэтому еще один способ добавить текст и изображения - добавить subview. Пример:

// 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];

Ответ 3

UIButton *  selectCountryBtn =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];  

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

Здесь я создаю образец программной кнопки, а затем задаю заголовок для его состояния Normal Control. Вы можете использовать свой собственный объект кнопки для установки строки заголовка, кроме того, вы также можете установить текст заголовка для других состояний управления путем изменения UIControlStateNormal к другим требуемым состояниям.

Ответ 4

Для быстрых разработчиков -

button.setTitle("Your button Name", forState: .Normal)

Ответ 5

  self.Button_name.titleLabel.text = @"Your title name";