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

是一样的.
查看截图.

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...