Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView with custom font not working when text set to "attributed"

I have a UITextView with some text that came from a .rtf (pasted directly onto Xcode)

The context contain only one custom font (Futura Book BT 11.0)

If I set the "text(attributed)" property to "plain"= The custom font appear properly from the storyboard and from the app

If I set the "text" property to "attributed"=. The custom font appear properly from the storyboard BUT not from the app.

As my goal was to have a text with multiple font working, how to have the attributed property to work with custom fonts? (Swift)

Thanks!

like image 929
Franck Avatar asked Jun 28 '15 17:06

Franck


3 Answers

The custom font appear properly from the storyboard BUT not from the app.

Does the app not show the font at all, or does it restore to system font? If it is reverting to Helvetica, it looks like this may be a known bug in iOS 8.1 http://openradar.appspot.com/radar?id=5117089870249984

Possible Solution: Did you test it on the simulator and a device? It could be that it just doesn't work on the simulator, which means you could fix it by installing the font on your system.

like image 163
scott Avatar answered Oct 24 '22 04:10

scott


I have faced the same problem when I used multiple custom fonts in attributed UILabel. Storyboard shows correct result but device does not!!

What I have find out that if the font size is different of text with different custom fonts. It works fine!!

So for now I use font size with minor difference. Like if one is 14 I used 14.01 for the other. Its not possible inside storyboard so I opened storyboard as source code and manually set font size like:

<attributedString key="attributedTitle">    
  <fragment content="Do not have account? ">
      <attributes>
        <color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
        <font key="NSFont" size="14" name="Roboto-Light"/>
        <paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
      </attributes>
    </fragment>
    <fragment content="Login">
      <attributes>
        <color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
        <font key="NSFont" size="14.01" name="Roboto-Bold"/>
        <paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
      </attributes>
    </fragment>
<attributedString key="attributedTitle">

But still I am unable to find why it happens!

like image 8
Bilal Ahmad Avatar answered Oct 24 '22 03:10

Bilal Ahmad


What I do:

private var _mutableString: NSMutableAttributedString!

if !isTitle {
    var titleFont: UIFont = UIFont(name: StyleGlobals.TitleFontName, size: StyleGlobals.TitleFontSize)!
    self._mutableString.addAttribute(NSFontAttributeName, value: titleFont, range: NSMakeRange(0, count(self._mutableString.string)))
    self._mutableString.addAttribute(NSForegroundColorAttributeName, value: StyleGlobals.FontColor, range: NSMakeRange(0, count(self._mutableString.string)))
}

self.Label.attributedText = self._mutableString

This applies both a new font color as well as the font itself. I have a certain UILabel that is has to be able to contain different fonts. It can either be a Title or a subtitle. Hence I had to apply the font when I had determined what the Label is going to contain.

like image 4
Orion Avatar answered Oct 24 '22 04:10

Orion