我创建一个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.
更多推荐
发布评论