我创建了一个按钮网格。以下代码创建按钮并显示它们,但按钮上没有文本。我缺少一个设置吗? (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);
答案 0 :(得分:129)
我想你想要setTitle: forState:
[button setTitle:@"Baha'i" forState:UIControlStateNormal]
答案 1 :(得分: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];
答案 2 :(得分:3)
UIButton * selectCountryBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[selectCountryBtn setTitle:@"My Title" forState:UIControlStateNormal];
这里,我已经以编程方式创建了示例按钮,然后将其标题设置为其正常控制状态,您可以使用自己的按钮对象来设置标题字符串,此外,您还可以通过更改UIControlStateNormal来设置其他控件状态的标题文本到另一个必要的州。
答案 3 :(得分:0)
对于快速开发者 -
button.setTitle("Your button Name", forState: .Normal)
答案 4 :(得分:0)
在我的情况下(Swift 4)是:
btnLogin.setTitle("Welcome, " + var_name, for: [])
答案 5 :(得分:0)
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
答案 6 :(得分:-4)
self.Button_name.titleLabel.text = @"Your title name";