1

I know how to set font programmatically when it comes to things like system fonts or custom fonts. But how do I specify the body font style? or caption or headline, for that matter?

3 Answers 3

1

Here is one way, you can modify it for your need.

attributes:@{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}

For the others, replace body with say headline, etc.

0

You can go for setting attributed string using NSAttributedString.

You alloc and init the NSAttributed string object set the attributes like body fonts and or headline etc.

Moreover, you can set the HTML text in the attributed text.

I hope the following links might help you...

Example of NSAttributedString with two different font sizes?

http://sketchytech.blogspot.in/2013/11/making-most-of-uitextview-in-ios-7.html

0

In Swift, you have to create a font with the class function called preferredFont from UIFont:

let label1 = UILabel()
label1.font = UIFont.preferredFont(forTextStyle: .body)

let label2 = UILabel()
label2.font = UIFont.preferredFont(forTextStyle: .caption1)

let label3 = UILabel()
label3.font = UIFont.preferredFont(forTextStyle: .headline)

You can find all the text styles here:

UIFont.TextStyle documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.