Cody Harness Cody Harness - 2 years ago 531
Swift Question

Set label font with bold and italic

I'm trying to set a specific part of my label to bold and italic with

, 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() {

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


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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download