ios - 从 UILabel 切换到 UIButton。文字消失

标签 ios uibutton uilabel

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

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

UILabel *firstLabel;

(...)

firstLabel.textColor = [UIColor blackColor];

到:

UIButton *firstLabel;

(...)

firstLabel.titleLabel.textColor = [UIColor blackColor];

为了清楚起见,这里有一个完整的 block :

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

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

我会很高兴被证明是错误的,因为我想使用这个功能。

最佳答案

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

关于ios - 从 UILabel 切换到 UIButton。文字消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598522/

相关文章:

objective-c - NSOperation 中带有 block 的方法 - 它是如何工作的?

ios - 滚动后自定义 UICollectionView 单元格 layoutsubview 不正确

swift - Swift 中 HomeVC 退出按钮的导航 rootViewController 问题

c# - 如何在 Xamarin 中将特定数据更新到 SQLite

ios - UICollectionViewCell 中的 UILabel

ios - UILabel.attributedText 设置属性一次,然后只更改文本(字符串)

ios - 我的 iPhone 应用程序无法在新的 iPhone 5 模拟器中使用信箱运行。我怎么把它放到信箱里?

ios - 从 plist 文件中读取字符串

ios - 可以将 UIButton 图像设置为零索引吗?

ios - 在 UITextView 中定位 UILabel