![](/img/trans.png)
[英]How to set a default title in UIButton subclass that works in Interface Builder (Xcode 9)
[英]Attributed Title attributes set to UIButton in Interface Builder are ignored in app
我有一个UIButton,我在Interface Builder中给出了一个属性标题:它有一个特定的字体,字符串的一部分是不同的颜色。
当我加载我的应用程序时,颜色保持不变,但应用程序将字体恢复为系统默认值...
有人经历过这个或知道修复的方法吗? 试图避免在代码中设置属性标题。
如果重要,我使用的字体是Open Sans,它在我在代码中指定字体的其他标签上正确显示。
您是否尝试在更改按钮字体之前选择文本? ?
我一周前也有同样的问题。 我也在使用Open Sans。 但是每次我尝试构建时,都会遇到一个无处可寻的编译错误。
无论如何..我只是在按钮上添加了一个标签,并按照我想要的方式进行配置。
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];
我遇到了同样的问题,但是UIButton,我的UILable已经改为新的字符串,我确定它在Interface builder上有所改变。 但是构建时它仍然是旧字符串。 我在源代码中打开了Interface builder。 搜索旧字符串,它仍然在那里。 刚删除它来解决问题。 (我不知道为什么会这样)。 也许你应该打开IB以确保。
希望它对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.