我看到了一堆类似的问题,但没有人得到我正在寻找的答案。当我使用这段代码创建一个UIButton并将其设置为titleLabel时,会出现UIButton,但titleLabel不会出现。UIButton titleLabel没有显示
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
button.titleLabel.text = @"Title";
button.titleLabel.font = [UIFont fontWithName:@"System-Bold" size:25.0f];
[self.view addSubview:button];
此代码显示按钮,但不是titleView,它的标题。为什么会这样?我正在开发iOS 6和更高版本。
注1:我不是在寻找这样一个选项:
[button setTitle:@"Title" forState:UIControlStateNormal];
因为我必须使用titleLabel来设置它的字体和中文字体。
注意2:我无法创建UILabel并将其作为子按钮添加。由于该按钮后来有动画效果。
您应该使用'setTitleColor:forState'来设置颜色(而不是在'titleLabel'上调用'setTextColor')。 – hverlind