Richer text for UILabels

Swift

This post is silly simple, but in the past, I remember doing things like this using Ranges.  You have a 2-line UILabel and you want a bold font for the first line, and then regular for the second. In Swift, this is quite simple and straight-forward. This is all the code you’d need to pull it off easily.

let style = NSMutableParagraphStyle()
style.lineSpacing = 5
let attString = NSMutableAttributedString(string: "I am the first line.\n",
    attributes: [NSFontAttributeName: UIFont(name:"Gotham-Bold", size:18.0)!,
    NSParagraphStyleAttributeName: style])
attString.append(NSMutableAttributedString(string: "I am the second line.",
    attributes: [NSFontAttributeName: UIFont(name:"Gotham-Book", size:18.0)!,
    NSParagraphStyleAttributeName: style]))
label.attributedText = attString

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.