iPhone / iPad的UIButton TitleLabel文字没有出现
我创build了一个button的网格。 下面的代码创buildbutton并显示它们,但button上没有文本。 有没有我失踪的设置? (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);
我想你想setTitle: forState:
[button setTitle:@"Baha'i" forState:UIControlStateNormal]
一个UIButton从UIViewinheritance,所以添加文本和图像的另一种方式是添加子视图。 例:
// 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];
UIButton * selectCountryBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; [selectCountryBtn setTitle:@"My Title" forState:UIControlStateNormal];
在这里,我以编程方式创build了示例button,然后为其Normal Control状态设置标题,您可以使用自己的button对象设置标题string,另外还可以通过将UIControlStateNormal更改为另一个需要的标题文本来设置另一个控件状态状态。
对于迅速的开发者来说,
button.setTitle("Your button Name", forState: .Normal)
self.Button_name.titleLabel.text = @"Your title name";