微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 使用文本设置时,UILabel显示自定义字体错误,但在代码中设置时工作正常

因此,当我使用文本设置为Plain的UILabel时,自定义字体在应用程序和IB中正确显示,但是当我将其设置为属性时,它在IB中看起来很好但在应用程序中却没有.

我以编程方式使用自定义字体到处都有属性字符串,它工作得很好.

我已经通过字体书在我的mac上安装了字体,我已经通过在info.plist和构建阶段的copy resources部分中设置了适当的属性将字体安装到我的应用程序中.我也在使用Xcode 6.1.1

我已经包含了一个说明我问题的sample project

这是在IB中设置的XIB中的标签的HTML.如您所见,它已设置为自定义字体.

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" usesAttributedText="YES" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizetoFit="NO"
translatesAutoresizingMaskIntoConstraints="NO" id="kpn-Ch-m1y">
  <rect key="frame" x="20" y="84" width="560" height="100" />
  <constraints>
    <constraint firstAttribute="height" relation="greaterThanorEqual" constant="100" id="tyA-gf-Vjj" />
  </constraints>
  <attributedString key="attributedText">
    <fragment content="the attack of the chicken lover">
      <attributes>
        <font key="NSFont" size="11" name="HelveticaNeueLTStd-Cn" />
        <font key="NSOriginalFont" size="11" name="HelveticaNeueLTStd-Cn" />
        <paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" />
      </attributes>
    </fragment>
  </attributedString>
  <nil key="highlightedColor" />
  <variation key="heightClass=compact" ambiguous="YES">
    <rect key="frame" x="23" y="84" width="554" height="211" />
  </variation>
  <variation key="heightClass=compact-widthClass=compact" ambiguous="YES">
    <rect key="frame" x="23" y="84" width="354" height="211" />
  </variation>
</label>

以下是当您打印叛逆标签内容时发生的情况,该标签的IB是在IB中设置的.正如你可以看到XIB之间发生的事情,它是一个浓缩的helvetica字体,它是应用程序中的一种,一个helvetica常规字体.

2015-02-25 17:35:09.726 FontTest[25984:360263] Helvetica Nueue Condensed{
    NSColor = "UIDeviceWhiteColorSpace 0 1";
    NSFont = "<UICTFont: 0x792604b0> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
    NSParagraphStyle = "Alignment 0,Linespacing 0,ParagraphSpacing 0,ParagraphSpacingBefore 0,Headindent 0,TailIndent 0,FirstLineHeadindent 0,LineHeight 0/0,LineHeightMultiple 0,LineBreakMode 4,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";
}
(lldb)

解决方法

您在代码中设置的字体
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeueLTStd-Cn" size:17]};

在IB中将其更改为HelveticaNeueLTStd-Cn 17.0

是一样的.
查看截图.

我猜你没有设置第一张图片显示的字体.

原文地址:https://www.jb51.cc/iOS/331185.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐