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

iOS: UIButton titleLabel -- does it do anything at all?(iOS:UIButton titleLabel——它有什么作用吗?)
本文介绍了iOS:UIButton titleLabel——它有什么作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个 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?

推荐答案

像这样设置 titleLabeltext 属性没有任何效果.相反,在按钮上调用 -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]

这是因为按钮可以为不同的状态有不同的标题(例如,UIControlStateDisabledUIControlStateHighlighted).如果您没有明确指定其他状态,则为 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 上的其余属性,例如 textAlignmentfont,应该按照您现在设置的方式工作,并且应该应用于所有控件州.

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.

具体而言,请参阅 UIButtontitleLabel 属性的文档: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——它有什么作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)