iphone - 为什么这个 UIButton 不显示它的文本标签?

标签 iphone cocoa-touch uibutton

这个 UIButton 的所有内容都呈现得很好,除了上面应该显示的文本。 NSLog 表明文本位于正确的位置。给出了什么?

UIButton *newTagButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newTagButton addTarget:self action:@selector(showNewTagField) forControlEvents:UIControlEventTouchUpInside];
newTagButton.titleLabel.text = @"+ New Tag";
NSLog(@"Just set button label to %@", newTagButton.titleLabel.text);
newTagButton.titleLabel.font = [UIFont systemFontOfSize:17];
newTagButton.titleLabel.textColor = [UIColor redColor];
CGSize addtextsize = [newTagButton.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];
CGSize buttonsize = { (addtextsize.width + 20), (addtextsize.height * 1.2) };
newTagButton.frame = CGRectMake(x, y, buttonsize.width, buttonsize.height);
[self.mainView addSubview:newTagButton];

最佳答案

UIButton 上有一组 API 可用于更改这些属性。

titleLabel 可以并且将会由 UIButton 在内部进行更改。

[button setTitle:title forState:state];
[button setTitleColor:color forState:state];
[button setTitleShadowColor:color forState:state];

您应该始终通过这些方法(如果可用)设置这些属性,而不是直接触摸 titleLabel。对于字体,您可以直接在 titleLabel 上更改它,因为它们没有在 UIButton 上提供方法。

关于iphone - 为什么这个 UIButton 不显示它的文本标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3022884/

相关文章:

ios - 使 UIButton 大于其图像

iphone - iOS 图像压缩算法 - 我将如何创建一个?

iphone - 如何只显示有电话号码的联系人?

cocoa - 对 Xcode 6 中创建胖静态库的不同过程感到困惑

iphone - 如何设置 UIButton 类型 RoundedRect 按钮的背景颜色?

ios - 如何为 swift 3 中以编程方式定义的按钮设置监听器函数

iphone - 如何检查 iPhone 上的自定义 url 方案?

iphone - 奇怪的架构,没有 appDelegate 类的应用程序。 (AVCam 源链接)

iphone - iCloud : how to manage iCloud deactivation?

iphone - 如何在 iOS 应用程序中以编程方式调用电话卡号码并输入 PIN?