vo-q
12/1/2017 - 3:22 AM

Custom style char in string

extension NSMutableAttributedString {
    @discardableResult func bold(_ text:String) -> NSMutableAttributedString {
        let attrs:[NSAttributedStringKey:Any] = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17, weight: .bold)]
        let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
        self.append(boldString)
        return self
    }
    
    @discardableResult func normal(_ text:String)->NSMutableAttributedString {
        let attrs:[NSAttributedStringKey:Any] = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17, weight: .regular)]
        let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
        self.append(boldString)
        return self
    }
}

extension UILabel {
    func setTextAttibutedStringPhone(_ content: String, _ phone: String) {
        let formattedString = NSMutableAttributedString()
        formattedString
            .normal(content)
            .bold(phone)
        self.attributedText = formattedString
    }
}