【问题标题】:Switched from UILabel to UIButton. Text disappears从 UILabel 切换到 UIButton。文字消失
【发布时间】:2014-05-11 22:14:20
【问题描述】:

我刚从UILabel切换到UIButton,打算利用按钮的titleLabel属性来显示以前显示在按钮替换的标签中的信息。

问题是,文本不显示。我检查了按钮本身(通常具有透明背景,更改为红色以进行检查)是否出现,它是,但文本不存在。这是相关代码的示例,它与标签中的原始(工作)代码相同,仅更改如下行:

UILabel *firstLabel;

(...)

firstLabel.textColor = [UIColor blackColor];

到:

UIButton *firstLabel;

(...)

firstLabel.titleLabel.textColor = [UIColor blackColor];

为了清楚起见,这是一个完整的块:

         firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
         firstLabel.backgroundColor = [UIColor clearColor];
         firstLabel.titleLabel.textColor = [UIColor blackColor];
         firstLabel.titleLabel.text = [NSString stringWithFormat:@"%@\n%.2f%%\nTime--%@",focusItemName,focusItemPercent,actualDurationFocusItem];
         [firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
         firstLabel.titleLabel.numberOfLines = 0;
         firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
         [firstLabel setHidden:NO];

我错过了什么?有什么想法吗?

谢谢!

更新—— 这可能与已知错误有关!

非常感谢下面的响应者。我实施了第一个建议的修复,它解决了我最初的问题,所以我的文本现在出现在标签中。但是,UIControlStateHighlighted 和 UIControlStateSelected 不起作用。

例如,此代码在单击时不会对文本产生明显影响:

firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

在 SO 上搜索了一段时间后,我得出的结论是存在一个错误(至少在 iOS 7 中),它阻止了这些方法的正常运行。不幸的是,我不够聪明,无法提供更详细的信息,但最近的很多帖子都指出了 UIControl 领域的一个主要问题。

被证明是错误的我会欣喜若狂,因为我想使用这个功能。

【问题讨论】:

  • 你试过–setTitle:forState: 吗?

标签: ios uibutton uilabel


【解决方案1】:

UIButton响应不同的消息,你可以使用按钮状态来改变它的标题。

你可以这样设置标题;

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:@"Title goes here" forState:UIControlStateNormal];

查看控制状态:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState

【讨论】:

  • 好的——你建议的代码很好用!然而,新的问题出现了。我希望在用户点击标签时更改文本颜色(除了发送到将显示详细信息的新视图控制器之外)。我试过[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];,但没有任何反应。
【解决方案2】:

要在UIButton 中设置标题,您必须初始化按钮:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];

然后使用:

[button setTitle:@"text" forState:UIControlStateNormal];

必须具体说明控件状态(突出显示选择等)

【讨论】:

  • 您应该选择正确的答案来关闭首先打开的票证。然后我们还可以看到其他的东西。
  • 在任何情况下,代码都没有错误..现在测试并且可以正常工作。你有什么问题?您将颜色设置为白色,所以也许您看不到它,因为它位于白色背景上?我不知道...你把那个代码放在哪里了?
  • 你说得对,马特奥。我应该把这两个问题分开。我将在一个新问题中发布另一个问题。
  • 您好已经在这里回复了您的问题..您的代码没有问题。我问你,你在哪里使用那个代码......在什么时候。所以无论如何,请设置正确的答案。
  • 我将代码放在创建按钮的同一个函数中。我一定是误解了一些关键的东西。你能展示你用来测试它的代码吗,或者告诉我你把它放在哪里?
【解决方案3】:

例如,尝试,

UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:@"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color


【讨论】:

  • 我试过这个——有变体——但无济于事。我有另一个问题(链接器错误)正在追踪,但很快就会返回。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
相关资源
最近更新 更多