Adama
Adama

Reputation: 1097

Attributed Title attributes set to UIButton in Interface Builder are ignored in app

I have a UIButton that I've given an attributed title in Interface Builder: it has a specific font, and one part of the string is a different color.

When I load my app, the color is maintained, but the app reverts the font back to system default...

Anyone experience this or know a way to fix? Was trying to avoid having to set the attributed titles in code.

If it matters, the font I'm using is Open Sans, and it displays correctly on other labels where I've specified the font in code.

Upvotes: 13

Views: 2715

Answers (3)

Mohit Tomar
Mohit Tomar

Reputation: 5201

 NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [style setAlignment:NSTextAlignmentCenter];
    [style setLineBreakMode:NSLineBreakByWordWrapping];

    NSDictionary *dict1 = @{NSFontAttributeName:[UIFont fontWithName:@"Varela Round" size:15.0f],
                            NSForegroundColorAttributeName:[UIColor whiteColor],
                            NSParagraphStyleAttributeName:style};
    NSDictionary *dict2 = @{NSFontAttributeName:[UIFont fontWithName:@"Varela Round"  size:10.0f],
                            NSForegroundColorAttributeName:[UIColor whiteColor],
                            NSParagraphStyleAttributeName:style};

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Expensed\n"    attributes:dict1]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Tap to edit"      attributes:dict2]];
    [cell.expenseTripButton setAttributedTitle:attString forState:UIControlStateNormal];
    [[cell.expenseTripButton titleLabel] setNumberOfLines:0];
    [[cell.expenseTripButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];

Upvotes: 0

Che
Che

Reputation: 974

Have you tried to select text before you change button's font?example?

Upvotes: 2

RoHiguera
RoHiguera

Reputation: 118

I had the same issue a week ago. I was using Open Sans, too. But every time I tried to build I got a compilation error that pointed nowhere.

Anyway.. I just added a label to the button and configured it the way I wanted.

Upvotes: 0

Related Questions