iPhone/iPad UIButton TitleLabel 文本未出现

发布于 2024-08-24 21:43:43 字数 478 浏览 4 评论 0原文

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

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 fine, I'm bi-lingual)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

‖放下 2024-08-31 21:43:43

我想你想要 setTitle: forState:

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

I think you want setTitle: forState:

[button setTitle:@"Baha'i" forState:UIControlStateNormal]
太傻旳人生 2024-08-31 21:43:43

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

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];
愛放△進行李 2024-08-31 21:43:43
UIButton *  selectCountryBtn =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];  

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

在这里,我以编程方式创建示例按钮,然后设置其普通控件状态的标题,您可以使用自己的按钮对象来设置标题字符串,此外,您还可以通过将 UIControlStateNormal 更改为另一个所需的值来设置另一个控件状态的标题文本州。

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.

捎一片雪花 2024-08-31 21:43:43

就我而言(Swift 4)如下:

btnLogin.setTitle("Welcome, " + var_name, for: [])

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

btnLogin.setTitle("Welcome, " + var_name, for: [])
两仪 2024-08-31 21:43:43

对于快速开发者-

button.setTitle("Your button Name", for: .normal)

For swift Developers-

button.setTitle("Your button Name", for: .normal)
赴月观长安 2024-08-31 21:43:43
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
人生戏 2024-08-31 21:43:43
  self.Button_name.titleLabel.text = @"Your title name";
  self.Button_name.titleLabel.text = @"Your title name";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文