How do I make an attributed string using Swift?

前端 未结 28 2155
耶瑟儿~
耶瑟儿~ 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:54

    extension UILabel{
        func setSubTextColor(pSubString : String, pColor : UIColor){    
            let attributedString: NSMutableAttributedString = self.attributedText != nil ? NSMutableAttributedString(attributedString: self.attributedText!) : NSMutableAttributedString(string: self.text!);
    
            let range = attributedString.mutableString.range(of: pSubString, options:NSString.CompareOptions.caseInsensitive)
            if range.location != NSNotFound {
                attributedString.addAttribute(NSForegroundColorAttributeName, value: pColor, range: range);
            }
            self.attributedText = attributedString
        }
    }
    

提交回复
热议问题