titleLable.text不起作用

vig*_*mar 2 uibutton ios

我创建了一个UIButton并用于titleLable.text设置文本,但是在按钮中看不到该文本,因此我将其替换为setTextmethod,它显示了文本,我只想知道为什么该按钮titleLable.text不起作用

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.text=@"text";
Run Code Online (Sandbox Code Playgroud)

然后像下面一样使用

    [button setTitle:@"text" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

Mid*_* MP 5

因为titleLabelreadonly财产。

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.
Run Code Online (Sandbox Code Playgroud)

在UIButton.h中声明

请参考UIButton类以获取更多详细信息。