iOS:UIButton titleLabel——它有什么作用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4910446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:48:37  来源:igfitidea点击:

iOS: UIButton titleLabel -- does it do anything at all?

iphoneiosuibuttonuilabeluifont

提问by Amagrammer

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.

我想制作一个 UIButtonTypeCustom 类型的 UIButton(用于形状)。我想使用 button.titleLabel 分配标题,因为我需要指定字体。下面的代码看起来应该可以工作,但没有 - 没有标签显示,句号。

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

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.

每个人都说要使用 setTitle:forState: 但这会给你一种我不喜欢的字体。titleLabel 方法没有被弃用——它应该可以工作。

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?

我以前遇到过几次,并且总是解决它,但我真的很想弄清楚。有任何想法吗?

回答by CIFilter

Setting the titleLabel's textproperty like that has no effect. Instead, call -setTitle:forState:on the button:

像这样设置titleLabeltext属性没有任何效果。相反,调用-setTitle:forState:按钮:

[editButton setTitle:@"Edit" forState: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 UIControlStateNormalcontrol state will apply to all the states if you don't specify the others explicitly.

这样做的原因是因为该按钮可以有不同的状态不同的标题(如UIControlStateDisabledUIControlStateHighlighted)。设置属性为UIControlStateNormal控制状态也适用于所有的州,如果你没有明确指定的其他人。

Per the documentation for UIButton:

根据以下文档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.

此类提供用于设置按钮的标题、图像和其他外观属性的方法。通过使用这些访问器,您可以为每个按钮状态指定不同的外观。

You can customize label's color and shadow color based on the state. See -setTitleColor:forStateand -setTitleShadowColor:forState, respectively. The rest of the properties on titleLabel, such as textAlignmentand font, should work as you have them set now and should apply to all the control states.

您可以根据状态自定义标签的颜色和阴影颜色。分别参见-setTitleColor:forState-setTitleShadowColor:forState。上的其余属性titleLabel,例如textAlignmentand font,应该按照您现在设置的方式工作,并且应该适用于所有控件状态。

Specifically, see the documentation for UIButton's titleLabelproperty: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel

具体请参见UIButtontitleLabel属性文档:https: //developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel

titleLabelitself 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.

titleLabel本身是只读的,但这并不意味着您不能更改其自身属性的值,例如字体、换行模式等。

回答by raddevus

Here's how I worked it out using Swift 4.2.

这是我使用 Swift 4.2 解决的方法。

counterButton.setTitle("Start Counter",
                      for:UIControl.State.normal)