属性标题未在 xib 文件的 UIButton 中加载自定义字体
Posted
技术标签:
【中文标题】属性标题未在 xib 文件的 UIButton 中加载自定义字体【英文标题】:attributed title not loading custom font in UIButton in xib file 【发布时间】:2015-05-08 20:46:14 【问题描述】:我正在努力解决这个问题,但不知道出了什么问题。我的一个界面构建器 xib 中有一个按钮,该按钮具有标题的属性字符串,该标题使用添加到我的应用程序中的自定义字体。界面生成器中的一切看起来都很好。
我 100% 确信我已将字体 (Montserrat) 正确加载到我的应用程序中。我可以在整个代码和控件的非属性标题中正确使用它,它工作正常。我还检查了调试器并正确列出了字体:
(lldb) po [UIFont familyNames]
<__NSCFArray 0x7bb34e20>(
Thonburi,
...
Montserrat,
...
Bodoni 72 Smallcaps
)
(lldb) po [UIFont fontNamesForFamilyName:@"Montserrat"]
<__NSCFArray 0x7be75400>(
Montserrat-Regular,
Montserrat-Bold
)
我还可以在文本编辑器的 xib 文件中看到,它看起来应该加载 Montserrat 的单词“got”:
<state key="normal">
<attributedString key="attributedTitle">
<fragment content="Already ">
<attributes>
<color key="NSColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<font key="NSFont" size="15" name="Helvetica"/>
<paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural"/>
</attributes>
</fragment>
<fragment content="got">
<attributes>
<color key="NSColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<font key="NSFont" size="15" name="Montserrat-Bold"/>
<paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural"/>
</attributes>
</fragment>
<fragment content=" an account? ">
<attributes>
<color key="NSColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<font key="NSFont" size="15" name="Helvetica"/>
<paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural"/>
</attributes>
</fragment>
<fragment content="SIGN IN!">
<attributes>
<color key="NSColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<font key="NSFont" size="15" name="Helvetica-Bold"/>
<font key="NSOriginalFont" size="15" name="Helvetica-Bold"/>
<paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural"/>
</attributes>
</fragment>
</attributedString>
</state>
但我可以看到 ios 在“got”一词中用“HelveticaNeueInterface-Regular”取代了我对“Montserrat-Bold”的使用。这是调试器的另一个摘录:
(lldb) po [self signInButton].titleLabel.attributedText
Already
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0x7bb41430> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
got
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0x7bb3ffe0> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
an account?
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0x7bb41430> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
SIGN IN!
NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
NSFont = "<UICTFont: 0x7bb3f6a0> font-family: \"Helvetica\"; font-weight: bold; font-style: normal; font-size: 15.00pt";
NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
UILabels、模拟器或设备、iOS 7 或 8 上似乎也发生了同样的问题。但我可以在非属性标题上使用 Montserrat。在这一点上我真的很难过。有人遇到过这个问题吗?
谢谢, 暗里
【问题讨论】:
【参考方案1】:我认为这是一个错误
遇到了这个雷达http://openradar.appspot.com/18716979
【讨论】:
以上是关于属性标题未在 xib 文件的 UIButton 中加载自定义字体的主要内容,如果未能解决你的问题,请参考以下文章
子类UIButton:属性未在super.init调用时初始化
在从 XIB 加载的自定义视图中修改 UIButton 的标题标签