11
votes

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.

3
Had exactly the same issue with a UILabel (attributed with a custom font in IB). Seems like a bug...Alladinian
Did you try if you can set it through code? And what version of iOS are you using? I have had an issue that certain attributes was not getting applied to labels,buttons, etc on 8.1 but worked fine in all other versions. Not sure why this is. Just want to know if this similarEdwin Abraham
Works fine in code. Was hoping to get it working from Interface Builder though. This is running on iOS 8.3 simulator. I'll have to try some different versions, I guess.Adama
in my experience the attributed string functionality in IB is sorely lacking. personally I would recommend doing this in code to save the headache.David Schwartz
You're not changing the text itself in code, are you? That could reset the attributes depending on how you go about it.Tim

3 Answers

2
votes

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

0
votes

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.

0
votes
 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];