Question

I am working on an application where I am calling the UITextFieldDelegate method:

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

I am successfully calling it, and within the method, I enable a particular button. This is also working fine. However, the problem I am facing is that I am unable to make the title on the button bold when the button is enabled. I set the font in Interface Builder, and I'm trying to bold the title programamtically. Here is the relevant code that shows what I am doing:

        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

        }

Ignore the "if" clause itself. I want the text on the button to be bold when the button is enabled, and I want the text on the button to be "normal", when it is disabled. What am I doing wrong?

Was it helpful?

Solution 2

_doneButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];

this will make your text bold, because you are using system font, which is helvetica. I hope it helps

OTHER TIPS

If you're using the standard fonts and want to make it bold, this is cleaner:

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

This way, you don't have to worry about the font family or size.

Swift 3

doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 28, weight: UIFontWeightBold)

The following weights are available:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack

Note Apple's documentation, though:

// Beware that most fonts will _not_ have variants available in all these weights!

Similar to BooTooMany's answer, but I found that it actually changed the size when used in a UIButton.

I used:

[_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:_doneButton.titleLabel.font.pointSize]];

That way, it keeps the same font size as what it started with, just makes it bold

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top