简体   繁体   English

在应用程序中忽略在Interface Builder中设置为UIButton的归属标题属性

[英]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. 我有一个UIButton,我在Interface Builder中给出了一个属性标题:它有一个特定的字体,字符串的一部分是不同的颜色。

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. 如果重要,我使用的字体是Open Sans,它在我在代码中指定字体的其他标签上正确显示。

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. 我也在使用Open Sans。 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. 我遇到了同样的问题,但是UIButton,我的UILable已经改为新的字符串,我确定它在Interface builder上有所改变。 But It's still old string when build it. 但是构建时它仍然是旧字符串。 I opened Interface builder in Source code. 我在源代码中打开了Interface builder。 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. 也许你应该打开IB以确保。

Hope it help you. 希望它对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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