108

How do you set the text for a UILabel to bold in Swift programmatically? My code so far:

    var label = UILabel(frame:theFrame)
    label.text = "Foo"
rmaddy
  • 314,917
  • 42
  • 532
  • 579
bhzag
  • 2,932
  • 7
  • 23
  • 39

2 Answers2

292

Use font property of UILabel:

label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)

or use default system font to bold text:

label.font = UIFont.boldSystemFont(ofSize: 16.0)
Cœur
  • 37,241
  • 25
  • 195
  • 267
codester
  • 36,891
  • 10
  • 74
  • 72
18

Use attributed string:

// Define attributes
let labelFont = UIFont(name: "HelveticaNeue-Bold", size: 18)
let attributes :Dictionary = [NSFontAttributeName : labelFont]

// Create attributed string
var attrString = NSAttributedString(string: "Foo", attributes:attributes)
label.attributedText = attrString

You need to define attributes.

Using attributed string you can mix colors, sizes, fonts etc within one text

PiotrDomo
  • 1,045
  • 10
  • 29
  • 9
    Technically correct, but slightly overkill. – nhgrif Jul 29 '14 at 23:09
  • 1
    To expand on nhgrif's comment. NSAttributedString makes sense in this case - http://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string-in-swift – Nitin Nain Mar 20 '16 at 17:28