Making text bold using attributed string in swift Making text bold using attributed string in swift ios ios

Making text bold using attributed string in swift


Usage:

let label = UILabel()label.attributedText =    NSMutableAttributedString()        .bold("Address: ")        .normal(" Kathmandu, Nepal\n\n")        .orangeHighlight(" Email: ")        .blackHighlight(" prajeet.shrestha@gmail.com ")        .bold("\n\nCopyright: ")        .underlined(" All rights reserved. 2020.")

Result:

enter image description here

Here is a neat way to make a combination of bold and normal texts in a single label plus some other bonus methods.

Extension: Swift 5.*

extension NSMutableAttributedString {    var fontSize:CGFloat { return 14 }    var boldFont:UIFont { return UIFont(name: "AvenirNext-Bold", size: fontSize) ?? UIFont.boldSystemFont(ofSize: fontSize) }    var normalFont:UIFont { return UIFont(name: "AvenirNext-Regular", size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)}        func bold(_ value:String) -> NSMutableAttributedString {                let attributes:[NSAttributedString.Key : Any] = [            .font : boldFont        ]                self.append(NSAttributedString(string: value, attributes:attributes))        return self    }        func normal(_ value:String) -> NSMutableAttributedString {                let attributes:[NSAttributedString.Key : Any] = [            .font : normalFont,        ]                self.append(NSAttributedString(string: value, attributes:attributes))        return self    }    /* Other styling methods */    func orangeHighlight(_ value:String) -> NSMutableAttributedString {                let attributes:[NSAttributedString.Key : Any] = [            .font :  normalFont,            .foregroundColor : UIColor.white,            .backgroundColor : UIColor.orange        ]                self.append(NSAttributedString(string: value, attributes:attributes))        return self    }        func blackHighlight(_ value:String) -> NSMutableAttributedString {                let attributes:[NSAttributedString.Key : Any] = [            .font :  normalFont,            .foregroundColor : UIColor.white,            .backgroundColor : UIColor.black                    ]                self.append(NSAttributedString(string: value, attributes:attributes))        return self    }        func underlined(_ value:String) -> NSMutableAttributedString {                let attributes:[NSAttributedString.Key : Any] = [            .font :  normalFont,            .underlineStyle : NSUnderlineStyle.single.rawValue                    ]                self.append(NSAttributedString(string: value, attributes:attributes))        return self    }}

Note: If compiler is missing UIFont/UIColor, replace them with NSFont/NSColor.


var normalText = "Hi am normal"var boldText  = "And I am BOLD!"var attributedString = NSMutableAttributedString(string:normalText)var attrs = [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]var boldString = NSMutableAttributedString(string: boldText, attributes:attrs)attributedString.append(boldString)

When you want to assign it to a label:

yourLabel.attributedText = attributedString


edit/update: Xcode 8.3.2 • Swift 3.1

If you know HTML and CSS you can use it to easily control the font style, color and size of your attributed string as follow:

extension String {    var html2AttStr: NSAttributedString? {        return try? NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)    }}"<style type=\"text/css\">#red{color:#F00}#green{color:#0F0}#blue{color: #00F; font-weight: Bold; font-size: 32}</style><span id=\"red\" >Red,</span><span id=\"green\" > Green </span><span id=\"blue\">and Blue</span>".html2AttStr