【问题标题】:Unable to set Title of UIButton无法设置 UIButton 的标题
【发布时间】:2012-07-21 09:22:31
【问题描述】:

我是 iPhone 新手,

    xpos=30;
    ypos=40;

for(int i=0;i<[MyArray count];i++)
        {            
            if(i!=0)
            {
                if (i%4==0) 
                {                    
                    ypos+=180;
                    xpos=30;
                }
                else
                {
                    xpos+=200;
                }
            }
            
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(xpos, ypos, 110.0, 130.0);
            [button setImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)];
            [button setTitle:[NSString stringWithFormat:@"%@", [MyArray objectAtIndex:i]] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [self.view addSubview:button];
}

我可以看到按钮,但在 UIButton 上看不到我的文字。

我们将不胜感激。

【问题讨论】:

  • 我认为由于您的 xpos 和 ypos,您在按钮上的文字不可见。可能会失焦。
  • 删除这个 > [button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)];并尝试 agian。
  • 您的标题插图似乎比按钮的大小大得多。这使您在 te 按钮内的标题没有可用像素。
  • 不是因为 setImage 函数。看我的回复。插图可能是问题,但真正的问题不是那个。
  • @waheeda:真的很感谢你。你教会了我一些新东西:)

标签: iphone ios ipad uibutton


【解决方案1】:

请设置按钮背景图片。您已设置按钮图片。 请使用这个:

[button setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];

用于包装按钮标题用户:

[button setTitle:@"Test button name 123" forState:UIControlStateNormal];

 button.titleLabel.numberOfLines=2;

 button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;

【讨论】:

  • 我尝试使用setBackgroundImage,它工作正常,但文本被换行,我不想换行。
  • 我已经编辑了两行设置按钮标题的代码。设置按钮大小与图像大小相同。
  • 设置 button.tag=i;当按钮单击事件获取标签时。 NSLog(@"%@",[MyArray objectAtIndex:btn.tag]);
  • 最好将字体大小更改为更小,如果您不想换行,请删除该行 button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
【解决方案2】:

您看不到文字,因为您使用了 setImage: 按钮的功能。一次您可以在文本上显示图像或按钮,但您可以添加图像作为背景,这样您就可以看到图像和标题。简而言之,将图像设置为背景。

[btn setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];

请同时删除设置插图的行。

【讨论】:

  • 我尝试使用setBackgroundImage,它工作正常,但文本被换行,我不想换行。
  • 由于网络漫游而无法查看 :)
  • 我也试过了,button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(xpos, ypos, 110.0, 130.0); [按钮 setImage:[UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
  • 我不知道为什么您的图像看起来像挤压并且文本超出了按钮。
  • booklbl = [[[UILabel alloc] initWithFrame:CGRectMake(xpos, ypos, 70, 80)]autorelease]; booklbl.lineBreakMode = UILineBreakModeWordWrap; booklbl.numberOfLines = 0; booklbl.backgroundColor = [UIColor blueColor]; booklbl.text =@"222";[按钮 addSubview:booklbl]; [self.view addSubview:button];
【解决方案3】:

而不是设置

[button setImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];   // This will not allow you to set Button title.

你必须使用:

[button setBackgroundImage: [UIImage imageNamed:@"ibook2.png"] forState:UIControlStateNormal];    // This will allow you to set title along background button image.

【讨论】:

  • 我尝试使用setBackgroundImage,它工作正常,但文本被换行,我不想换行。
  • @Krunal : 你能发一下截图吗?
  • @Krunal: 可能你的文本长度会比你的按钮宽度大。
  • @Krunal : 你的文字比按钮的宽度长。
【解决方案4】:

如果您的图像太大,那么它只会显示图像,并且文本会因图像而被截断,因为文本将在图像之后可见,请尝试使用 setBackground 图像或尝试使用小分辨率图像或 setImageEdgeInsets 。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    相关资源
    最近更新 更多