- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义 UIView 的问题,我有一个 UIButton subview ,我想根据这样的一些条件在初始化时设置按钮的文本:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
self.testButton.titleLabel.text=@"Some Title";
}
没有任何反应,按钮的文本与 nib 文件中定义的相同,但是如果我将实现更改为:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
[self.testButton setTitle:@"Some Title" forState:UIControlStateNormal];
}
它按预期工作。
有人可以向我解释这两种方法之间的区别吗?什么时候使用它们?
编辑:
建议的答案不能解释我的情况,我测试了从另一个按钮的操作更改按钮的文本,如下所示:
- (IBAction)otherButtonClicked:(id)sender {
self.testButton.titleLabel.text=@"Some Title";
}
并且按钮的文本发生了变化。我只想了解这种行为。
最佳答案
正确答案是
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.
The titleLabel property returns a value even if the button has notbeen displayed yet. The value of the property is nil for systembuttons.
Use this method to set the title for the button. The title you specifyderives its formatting from the button’s associated label object. Ifyou set both a title and an attributed title for the button, thebutton prefers the use of the attributed title over this one.
At a minimum, you should set the value for the normal state. If atitle is not specified for a state, the default behavior is to use thetitle associated with the UIControlStateNormal state. If the value forUIControlStateNormal is not set, then the property defaults to asystem value.
关于iOS SDK : Difference between UIButton setTitleForState and UIButton titleLabel. 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746335/
我正在尝试更改按钮类的字体(在启动按钮之前),但它不起作用。看来“titleLabel”是问题所在,因为它为零。 guard let object = NSClassFromString(button
我的 ios 项目中有一个按钮,如果用户是当前用户,则显示编辑配置文件;如果用户不是当前用户,则显示关注。 我根据上述条件以编程方式设置 titleLabel 文本。我还以编程方式将按钮文本设置为居中
如何将 UIButton titleLabel 文本显示到其框架的底部。目前我正在使用 UIEdge 上边距并设置负 UIButton 图像宽度,结果不符合预期。我还想将图像中心放置在其框架上。` l
我需要将按钮的文本与变量进行比较。 @IBAction func CheckifATrue(sender: AnyObject) { if let thisbutton = sender.ti
我正在尝试将 UIButton 的 titleLabel 旋转 45 度。文本中的字符是“+”。 我可以获得要旋转的标题 button.titleLabel?.transform = CGAffine
我尝试使用: button.titleLabel?.textAlignment = .left 左对齐按钮上的文本,但文本仍显示在中间。我没有任何代码可以将按钮的标题设置为按钮的中心,因此默认情况下它
我在我的应用程序中观察到一个奇怪的趋势:每当显示警告消息时,导航栏按钮的标题值都会更改为其 Storyboard 标题值。这是 UIAlertView 之一: 上图中可见的标题值 100 是我的 St
我有一个连接到 UIButton 的 @IBOutlet。在我的代码中,我有以下方法,每当用户执行操作时都会执行该方法。我已经设法用一些虚拟函数替换了我的代码,以向大家展示我的问题所在。 为了重现这个
我写了一个按钮,我想在触摸按钮时更改其文本颜色。代码如下: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [supe
这段代码似乎运行良好,修改了我的按钮的属性,除了 titleLabel 字体颜色没有改变。我可以更改字体、字体大小、阴影,但不能更改颜色。尽管我试图将其设置为黑色,但它目前显示为白色。我试过 butt
我尝试设置 titleLabel.text 但它不会从默认标题文本更改的奇怪问题。我已经在调试器中逐步检查它并检查了 taskTitle 值,它是正确的新值,但 titleButton.titleLa
奇怪的事情发生了。基本上,我正在尝试重新创建消息传递应用程序。因此,当我尝试让“发送”按钮在用户输入至少 1 个字符时从灰色变为蓝色时。 当我尝试更改 titleLabel 时,问题就来了,按钮将消失
由于某些原因,我无法使 titleLabel 正确适合 UIButton。 适合宽度,但不适用于高度(这应该归功于baselineAdjustment) let btn = UIButton(fram
所以我在我的 Storyboard 中使用了尺寸等级。我的 Storyboard 中的按钮正在使用自动布局,一切正常。 我为我的 ViewController 创建了一个导出,我正在尝试更改标题标签文
想知道如何设置 UIButton title Label 中的文字自上而下。 文字是“Press Me”横跨 会喜欢展示 " p r e s s m e " 我做了这个 CGAffineTransfo
我正在提交这个简单的动画,它会导致按钮缩小到 0 的宽度: [UIView animateWithDuration:0.2 delay: 0.0
我的自定义按钮包含一个在滑动时更新的值。但是当我点击它时,它没有保留该值,而是设置回在 Interface Builder 中或通过 [button00 setText:@"default value
在 tableView 页脚我有一个按钮 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger
我在界面生成器中添加了按钮并引用了它们,然后我想更改它们的标题字体,但它确实有效。当我使用时: [_button setFont:[UIFont fontWithName:APP_FONT_FUTUR
UIButton 的 titleLabel 是可选的原因是什么? 通过放置感叹号来隐式解包这个可选是否安全?如果不是,那么当 titleLabel 为 nil 时会怎样? 顺便说一句,我使用 UIBu
我是一名优秀的程序员,十分优秀!