iOS:UIButton titleLabel - 它有什么用呢? [英] iOS: UIButton titleLabel -- does it do anything at all?
问题描述
我想制作一个类型为UIButtonTypeCustom的UIButton(用于形状)。我想使用button.titleLabel分配标题,因为我需要指定字体。以下代码看起来应该可以工作,但不会 - 没有标签显示,句点。
I want to make a UIButton with type UIButtonTypeCustom (for the shape). I want to assign the title using button.titleLabel because I need to specify the font. The following code looks like it should work, but doesn't -- no label shows up, period.
UIImage *editButtonImage = [UIImage imageNamed: @"editButton.png"];
float width = editButtonImage.size.width;
float height = editButtonImage.size.height;
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
每个人总是说要使用setTitle:forState:但这会给你一个我不喜欢的字体。 titleLabel方法不被弃用 - 它应该可以工作。
Everyone always says to use setTitle:forState: but that gives you a font I don't like. The titleLabel method is NOT deprecated -- it should work.
我之前已经遇到过好几次并且总是只是解决它,但我真的很想出来。有什么想法?
I have run into this several times before and always just worked around it, but I'd really like to figure it out. Any ideas?
推荐答案
设置 titleLabel
的 text
这样的属性没有效果。而是在按钮上调用 -setTitle:forState:
:
Setting the titleLabel
's text
property like that has no effect. Instead, call -setTitle:forState:
on the button:
[editButton setTitle:@"Edit" forState:UIControlStateNormal]
原因是因为按钮可以有不同状态的不同标题(例如, UIControlStateDisabled
, UIControlStateHighlighted
)。如果您没有明确指定其他状态,则为 UIControlStateNormal
控件状态设置属性将适用于所有状态。
The reason for this is because the button can have different titles for different states (e.g., UIControlStateDisabled
, UIControlStateHighlighted
). Setting a property for the UIControlStateNormal
control state will apply to all the states if you don't specify the others explicitly.
根据 UIButton
的文档:
此类提供设置方法按钮的标题,图像和其他外观属性。通过使用这些访问器,您可以为每个按钮状态指定不同的外观。
This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.
您可以根据标签的颜色和阴影颜色自定义州。分别参见 -setTitleColor:forState
和 -setTitleShadowColor:forState
。 titleLabel
上的其他属性,例如 textAlignment
和 font
,应该正常工作,应该适用于所有控制状态。
You can customize label's color and shadow color based on the state. See -setTitleColor:forState
and -setTitleShadowColor:forState
, respectively. The rest of the properties on titleLabel
, such as textAlignment
and font
, should work as you have them set now and should apply to all the control states.
具体来说,请参阅 UIButton的文档
's titleLabel
property: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel
Specifically, see the documentation for UIButton
's titleLabel
property: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel
titleLabel
本身是只读的,但这并不意味着你不能改变它自己属性的值,比如字体,换行模式等。
titleLabel
itself is read-only, but that doesn't mean you can't change the values of its own properties, such as font, line break mode, etc.
这篇关于iOS:UIButton titleLabel - 它有什么用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!