How do I make an attributed string using Swift?

前端 未结 28 2147
耶瑟儿~
耶瑟儿~ 2020-11-22 10:11

I am trying to make a simple Coffee Calculator. I need to display the amount of coffee in grams. The \"g\" symbol for grams needs to be attached to my UILabel that I am usin

28条回答
  •  一整个雨季
    2020-11-22 10:55

    Swift 5

        let attrStri = NSMutableAttributedString.init(string:"This is red")
        let nsRange = NSString(string: "This is red").range(of: "red", options: String.CompareOptions.caseInsensitive)
        attrStri.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font: UIFont.init(name: "PTSans-Regular", size: 15.0) as Any], range: nsRange)
        self.label.attributedText = attrStri
    

提交回复
热议问题