Setting UILabel text to bold

IosSwiftUilabel

Ios Problem Overview


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"

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbhzagView Question on Stackoverflow
Solution 1 - IoscodesterView Answer on Stackoverflow
Solution 2 - Iossumofighter666View Answer on Stackoverflow