learner
learner

Reputation: 11820

iOS: set textview font to body programmatically

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?

Upvotes: 1

Views: 1929

Answers (3)

pableiros
pableiros

Reputation: 16090

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

Upvotes: 0

R_Developer
R_Developer

Reputation: 864

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

Upvotes: 0

Konsol Labapen
Konsol Labapen

Reputation: 2434

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.

Upvotes: 1

Related Questions