iPhone/iPad UIButton TitleLabel文字没有出现

Ian*_*ink 34 cocoa-touch uibutton uilabel xamarin.ios

我创建了一个按钮网格.以下代码创建按钮并显示它们,但按钮上没有文本.我缺少一个设置吗?(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);
Run Code Online (Sandbox Code Playgroud)

Tob*_*hen 129

我想你想要的 setTitle: forState:

[button setTitle:@"Baha'i" forState:UIControlStateNormal]
Run Code Online (Sandbox Code Playgroud)

  • 这是荒谬的button.titleLabel.text没有用,但[button setTile:.. forState:]的工作原理是希望它默认设置为正常状态或其他东西.评论? (2认同)
  • @Gmu他们发现很难在苹果实施 (2认同)

ddi*_*ego 6

UIButton继承自UIView,因此添加文本和图像的另一种方法是添加子视图.例:

// 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];
Run Code Online (Sandbox Code Playgroud)