简体   繁体   中英

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.

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

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.

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

I had the same problem with you but UIButton, My UILable has been changed to new string, and I sure it was changed on Interface builder. But It's still old string when build it. I opened Interface builder in Source code. Searching the old string, It's still be there. Just removed it to fix issue. (I don't know why this's happening). Maybe you should open IB to make sure.

Hope it help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM