7
votes

So the custom font shows up properly in the application and in IB when I use a UILabel with the text set to Plain, but when I set it to attributed, it looks fine in IB but not in the app.

I programmatically use the custom font everywhere with attributed strings and it works just fine.

I've installed the font on my mac through font book and I've installed the font into my app by setting the proper property in the info.plist and in the copy resources section of the build phases. I'm also using Xcode 6.1.1

I've included a sample project illustrating my problem

Here's the HTML for the label in the XIB that is set in IB. As you can see it's set to the custom font.

<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>

Here is what happens when you print the contents of the renegade label that had it's attributedString set in IB. As you can see something happened between the XIB which had it as a condensed helvetica font to what it is in the app, a helvetica regular font.

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) 

enter image description here

1
I have the same problem: set label to use 'attributed text' on IB, run app and label displays wrong font (sans-serif... perhaps system font?). Set back to 'plain text' and the specified font is rendered. Logging the label font's family name gives the specified font in both cases!Nicolas Miari
Yeah it's pretty obnoxious. I'll be strongly against any use of custom fonts in future projects. I also get errors in height calculations with custom fonts that I don't get with the out of the box iOS fonts, but that's another issue.Biclops

1 Answers

-1
votes

The font which you set in code

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeueLTStd-Cn" size:17]};

and in IB when you change it to HelveticaNeueLTStd-Cn 17.0

is same. See the screenshots.

enter image description here

I guess you haven't set the font as shown in the first image.