尝试粗体化UIButton的titleLabel以响应在iOS中调用的委托方法

sye*_*dfa 7 uibutton ios uitextfielddelegate

我正在研究一个我调用UITextFieldDelegate方法的应用程序:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { }
Run Code Online (Sandbox Code Playgroud)

我成功调用它,在方法中,我启用了一个特定的按钮.这也很好.但是,我面临的问题是,当按钮启用时,我无法在按钮粗体上创建标题.我在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

        }
Run Code Online (Sandbox Code Playgroud)

忽略"if"子句本身.当按钮启用时,我希望按钮上的文本为粗体,并且我希望按钮上的文本在禁用时为"正常".谁能看出我做错了什么?

在此先感谢所有回复的人.

Boo*_*any 30

如果您使用标准字体并希望将其设为粗体,则更清晰:

[_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];
Run Code Online (Sandbox Code Playgroud)

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


inc*_*iko 7

_doneButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
Run Code Online (Sandbox Code Playgroud)

这将使您的文本变为粗体,因为您使用的是系统字体,即helvetica.我希望它有所帮助


bfx*_*bfx 5

雨燕3

doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 28, weight: UIFontWeightBold)
Run Code Online (Sandbox Code Playgroud)

可以使用以下重量:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack
Run Code Online (Sandbox Code Playgroud)

不过请注意苹果的文档:

// Beware that most fonts will _not_ have variants available in all these weights!
Run Code Online (Sandbox Code Playgroud)