Swift: Programmatically make UILabel bold without changing its size?

user1269290 picture user1269290 · Oct 12, 2016 · Viewed 21.7k times · Source

I have a UILabel created programmatically. I would like to make the text of the label bold without specifying font size. So far I have only found:

UIFont.boldSystemFont(ofSize: CGFloat) 

This is what I have exactly:

let titleLabel = UILabel()
let fontSize: CGFloat = 26
titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabelFontSize)

But this way I am also setting the size. I would like to avoid that. Is there a way?

If there is no way, what would be a good workaround in Swift?

Thank you!

Answer

Chris Vig picture Chris Vig · Oct 12, 2016

Why not just:

titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabel.font.pointSize)