ios - 尝试加粗 UIButton 的 titleLabel 以响应在 iOS 中调用的委托(delegate)方法

标签 ios uibutton uitextfielddelegate

我正在开发一个调用 UITextFieldDelegate 方法的应用程序:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { }

我成功地调用了它,并且在该方法中,我启用了一个特定的按钮。这也很好用。但是,我面临的问题是启用按钮后我无法将按钮上的标题设为粗体。我在 Interface Builder 中设置了字体,并试图以编程方式将标题加粗。这是显示我在做什么的相关代码:

        if ([keys count] == [_tireName count]) {

            [_doneButton setEnabled:YES];//this line works
            [[_doneButton titleLabel] setFont:[UIFont boldSystemFontOfSize:28]];//this line does nothing


        } else if ([keys count] < [_tireName count]){

            [_doneButton setEnabled:NO];//this line works
            [[_doneButton titleLabel] setFont:[UIFont systemFontOfSize:28]];//this line does nothing

        }

忽略“if”子句本身。我希望按钮启用时按钮上的文本为粗体,我希望按钮上的文本在禁用时为“正常”。我做错了什么?

最佳答案

如果您使用的是标准字体并希望将其设为粗体,这会更简洁:

[_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];

这样,您就不必担心字体系列或大小。

关于ios - 尝试加粗 UIButton 的 titleLabel 以响应在 iOS 中调用的委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18836941/

相关文章:

ios - 使用按钮执行一段代码几秒/分钟?

ios - 在委托(delegate)方法中使用默认行为对 UITextField 进行子类化的设计方法

ios - UITextFieldDelegate 与 UITextField 控件事件

ios - 对于UIButton如何通过@selector传递多个参数

iphone - 不调用自定义 UIButton 的操作

ios - 在 UITextField 中关闭键盘

ios - 当视觉效果 View 位于其顶部时,过渡时显示意外的背景图像

ios - UITabBarController 在 Swift 语言中设置默认选项卡

ios - 删除 UIViewController 的正确方法

ios - Swift 无法分配给 NSData 类型的不可变值