How to add attributed string swift ?

If you have two diffrent string and want to have diffrent text style like colours, font then you can use NSAttributedString which can customise. In below example i have joined two string with diffrent fonts, colours into same label.

let mutableString = NSMutableAttributedString()
let attributedString = NSAttributedString(string:"Helllo... ",
                                   attributes:[NSAttributedString.Key.foregroundColor: UIColor.blue,
                                               NSAttributedString.Key.font: UIFont.systemFont(ofSize: 22) as Any])
mutableString.append(attributedString)
let attributedString2 = NSAttributedString(string:"Janesh",
                                   attributes:[NSAttributedString.Key.foregroundColor: UIColor.red,
                                               NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 30) as Any])
mutableString.append(attributedString2)

self.lbl.attributedText = mutableString

Leave a Comment

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