An attempt to boldly highlight the UIButton header in response to the delegation method invoked in iOS

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

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

I successfully call it, and inside the method I enable a specific button. This also works great. However, the problem I am facing is that I cannot make the title on the button bold when the button is on. I set the font in Interface Builder and I'm trying to make a bold title programmatically. Here is the relevant code that shows what I'm 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. I want the text on the button to be bold when the button is on, and I want the text on the button to be β€œnormal” when it is off. Can anyone understand what I'm doing wrong?

Thanks in advance to everyone who responds.

+4
source share
4 answers
 _doneButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:24]; 

this will make your text bold because you are using a system font that is helvetica. I hope this helps

+5
source

If you use standard fonts and want to make them 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.

+22
source

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 

Check out the Apple documentation:

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

Similar to BooTooMany's answer, but I found that it actually resized when used in UIButton.

I used:

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

This way, it keeps the same font size it started with, just makes it bold.

0
source

Source: https://habr.com/ru/post/1502495/


All Articles