Set label font with bold and italic - ios

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.

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

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

Related

How to set a "bold" dynamic font type for UILabel?

The Apple documentation says that for iOS an app can use text styles and dynamic font type so text size automatically adjusts to the users reading preferences.
I have a text style Body and set Dynamic Type to true so that it automatically adjust the text size. This works good.
This text I would also like to make appear bold. I haven't found a way yet how to achieve this either by code or in the Interface Builder.
Is there a way to achieve a "bold dynamic font type"?
Ok, it seems the documentation provides a hint. For using custom fonts, I consider a bold font as a custom font now, one can use the UIFontMetrics class.
If I pass a "bold custom font" to the scaledFontForFont method of UIFontMetrics class I get the desired result with the dynamic font type UIFontTextStyleBody.
UIFont* boldSystemFont = [UIFont boldSystemFontOfSize:17];
myLabel.font = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleBody] scaledFontForFont:boldSystemFont];
So far I only see this way using code. No idea if this could somehow be setup in the Interface Builder as well.

Can't find custom font when using attributed title

I have custom Chivo font, added via cocoapod.
So, in xib I can find it using Title Plain, as you can see:
But if I need to use Attributed Title (for example to use 2 types: regular and bold in same string),
I can't find Chivo font:
So how to fix this? I sure can use it from code, but wonder if it can work in described way.
Thanks for help!

NSAttributedString: letters interference with arabic font

I used arabic font called: "Hafs", it is from here:
http://fonts.qurancomplex.gov.sa/download/UthmanicHafs1Ver09Font.zip
I used it to draw Ayat from Holy Quran, but some letters are interfered with others, as shown in picture:
how to represent right text without interference?
thanks
EDIT:
here is another photo from "Pages" application to show you the original text, i coloured words that corrupted in iOS simulator by red colour
EDIT2:
I set attributed strings follows:
1- i paste the string into TextView in the storyboard.
2- i wrote this code in the related viewcontroller:
let font:UIFont! = UIFont(name: "KFGQPC Uthmanic Script HAFS", size: 30)
self.txt2.attributedText = NSAttributedString(string: txt2.text, attributes: [NSFontAttributeName:font])

Make part of an alertview bold

Lets say I want to make only the text "Software" bold. Is there a way to achieve this without a custom alertview?
self.subHeadingLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
Check out dynamic text in ios7 . it has six different font styles. There is a good article here

iOS: custom font for specific label

I looked at a few examples here on OS and also followed this one but what I wanted to know is how I can set a custom font for only one specific label.
This line is supposed to change the font:
[UIFont fontWithName:#"Cloister Black" size:64.0]
But I don't want everything to be affected. Any ideas how I do this?
Setting the font on a label is as easy as modifying that label's "font" property (and I've linked Apple's documentation for you).
E.G.:
labelIWantToModify.font = [UIFont fontWithName:#"Cloister Black" size:64.0];
And I'm hoping you're referring to that one specific label, and not a piece or part of the string that appears within the label, which is a separate (attributed string) thing.

Resources