5

I'm trying to set a specific part of my label to bold and italic with NSMutableAttributedString, but I can only get one or the other. The font that I'm using is Clear Sans. Setting the entire label's font on the storyboard to Bold Italic works fine, but I only need part of it. So I used

for family in UIFont.familyNames() {
    print("\(family)\n\t\(UIFont.fontNamesForFamilyName(family))")
}

to make sure it's there, and I found

Clear Sans
    ["ClearSans", "ClearSans-Bold", "ClearSans-Italic"]

For some reason, it's not in here but it is in the storyboard. So I tried to use this answer, but it crashes, I assume for the same reason that I didn't find ClearSans-BoldItalic, but I don't know why it's not in there.

Finally, I tried to just "stack" the two, like so

let bold = UIFont(name: "ClearSans-Bold", size: 20)
let italic = UIFont(name: "ClearSans-Italic", size: 20)

attributedCaption.addAttribute(NSFontAttributeName, value: bold!, range: (imageContainer.caption as NSString).rangeOfString(matchString))
attributedCaption.addAttribute(NSFontAttributeName, value: italic!, range: (imageContainer.caption as NSString).rangeOfString(matchString))

but all this does is use the last attribute, which is what I had expected it to do.

Is there some other method I can use to do this so that I get both bold and italic to show on the same part of the label?

Edit

I wanted to include my fonts that are in the project, so it's clear that I've already imported them. The second image is the list of fonts on the storyboard. While it's covered up, the family is in fact Clear Sans.

Installed fonts Storyboard options

Community
  • 1
  • 1
Cody Harness
  • 1,116
  • 3
  • 18
  • 37
  • 1
    Try downloading the `Clear Sans Bold Italic` font. You should then able to use `ClearSans-BoldItalic`. https://www.fontsquirrel.com/fonts/clear-sans – kye Apr 18 '16 at 18:57
  • 1
    You can't just create arbitrary font on the fly first by instantiating UIFontDescriptor object with desired symbolic traits and then instantiating a new font from that descriptor. The font you describe with descriptor must already be present in the system, whether it's a standard font or the one you added manually. So just download bold italic version as kye noted, add it to your project and use it's name to instantiate a new font. – Russian Apr 18 '16 at 19:11
  • @kye I just updated the question with a screenshot of my fonts folder. I've already had it in the project (that's why it shows up on the storyboard) but didn't think to include that in the question. Thanks! – Cody Harness Apr 18 '16 at 19:11
  • 1
    @Cody Harness have you added bold italic version to info.plist under UIAppFonts key? – Russian Apr 18 '16 at 19:15
  • @Russian That's it! I didn't even think of that, it's been so long since I added the fonts in. Thank you. If you want to post that as the answer then I'll gladly accept it. – Cody Harness Apr 18 '16 at 19:17
  • @Cody Harness What about deleting the question itself :) ? Or do you think it might be useful for someone else? – Russian Apr 18 '16 at 19:27
  • Honestly I think it would be useful for someone else looking around for a similar solution. – Cody Harness Apr 18 '16 at 19:31

2 Answers2

3

Make sure bold italic version is also added to app's Info.plist under UIAppFonts ("Fonts provided by application") key.

Russian
  • 1,296
  • 10
  • 15
1
UIFont.italicSystemFont(ofSize: 15.0)
UIFont.boldSystemFont(ofSize: 15)

if you want to custom font of label like fond system . you can try my solution . it works good for swift 3

Ace
  • 219
  • 1
  • 3