Swift: Make part of text bold

 Hello everyone,

You may have encountered situations where you want to display part of your label's string as bold, while keeping the other part as normal.


It's very easy using an attributed string. Below is the code that you need to accomplish this.


let attrs = [NSAttributedString.Key.font : UIFont(name: <your custom font name>, size: <your font size>)]


let attributedString = NSMutableAttributedString(string:boldText, attributes:attrs as [NSAttributedString.Key : Any])


let normalText = normalText

let normalString = NSMutableAttributedString(string:normalText)


attributedString.append(normalString)

        


Now, set the attributed string to your UILabel instance.


E.g

someLabel.attributedText = attributedString


That's it. You can play around with this and see how you can achieve other results with the attributed Strings!







Comments

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline