titleLable.text不起作用(titleLable.text not working)

编程入门 行业动态 更新时间:2024-10-23 13:30:03
titleLable.text不起作用(titleLable.text not working)

我创建一个UIButton并使用titleLable.text来设置文本,但是在按钮中看不到文本,所以我用setText方法替换它,它显示文本,我只是想知道为什么titleLable.text不起作用

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; button.titleLabel.text=@"text";

然后使用如下

[button setTitle:@"text" forState:UIControlStateNormal];

I create an UIButton and use titleLable.text to set text ,but the text is not seen in the button so I replace it with setText method, and it show the text, I just want to know why the titleLable.text is not working

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; button.titleLabel.text=@"text";

Then used like below

[button setTitle:@"text" forState:UIControlStateNormal];

最满意答案

因为titleLabel是一个readonly属性。

titleLabel

一个视图,显示按钮的currentTitle属性的值。 (只读)

@property(nonatomic, readonly, retain) UILabel *titleLabel >

讨论

虽然此属性是只读的,但它自己的属性是可读/写的。 主要使用这些属性来配置按钮的文本。 例如:

UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

button.titleLabel.font = [UIFont systemFontOfSize: 12];

button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;

不要使用标签对象来设置文本颜色或阴影颜色。 相反,使用setTitleColor:forState:和setTitleShadowColor:forState:此类的方法来进行这些更改。

即使尚未显示该按钮,titleLabel属性也会返回一个值。 系统按钮的属性值为nil。

可用性

Available in iOS 3.0 and later.

在UIButton.h中声明

有关更多详细信息,请参阅UIButton类 。

Because titleLabel is a readonly property.

titleLabel

A view that displays the value of the currentTitle property for a button. (read-only)

@property(nonatomic, readonly, retain) UILabel *titleLabel >

Discussion

Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:

UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

button.titleLabel.font = [UIFont systemFontOfSize: 12];

button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;

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 not been displayed yet. The value of the property is nil for system buttons.

Availability

Available in iOS 3.0 and later.

Declared In UIButton.h

Please refer UIButton Class for more details.

更多推荐

本文发布于:2023-08-05 17:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   titleLable   text   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!