在应用程序中,接口生成器中设置为UIButton的属性标题属性被忽略。

13
我有一个UIButton,在Interface Builder中设置了属性标题:它具有特定字体,字符串的一部分是不同的颜色。
当我加载我的应用程序时,颜色保持不变,但应用程序将字体恢复为系统默认...
有人遇到过这种情况或知道修复方法吗?我试图避免在代码中设置属性标题。
如果重要的话,我正在使用Open Sans字体,在其他我在代码中指定字体的标签上显示正确。

在使用IB中自定义字体属性的UILabel时,我遇到了完全相同的问题。看起来像是一个bug... - Alladinian
1
你尝试过通过代码来设置它吗?你使用的iOS版本是什么? 我曾经遇到过一个问题,即在8.1上某些属性无法应用于标签、按钮等,但在所有其他版本中都可以正常工作。不确定原因。只是想知道这是否类似。 - Edwin Abraham
在代码中正常运行。但是我希望能够从Interface Builder中让它工作。这是在iOS 8.3模拟器上运行的。我想我得尝试一些不同的版本。 - Adama
根据我的经验,IB中的属性字符串功能非常欠缺。个人建议在代码中完成以避免麻烦。 - David Schwartz
你不是在代码中直接更改文本内容,对吧?这样做可能会重置属性,具体取决于你的操作方式。 - Tim
3个回答

2

你试过在更改按钮字体之前选择文本吗?示例


1
当然。IB中字体显示正常,但在应用程序中却不正常。 - Adama

0

我在一周前也遇到了同样的问题。我也在使用Open Sans字体,但每次尝试构建时都会出现指向无处的编译错误。

不管怎样……我只是在按钮上添加了标签,并按照自己想要的方式进行了配置。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接