改變字串中的某字串顏色 === - swift 版本 ```swift= // 將 uploadCount 設為紅色 let text = "已為您上傳 \(uploadCount) 個項目。" let mutableAttributedString = NSMutableAttributedString(string: text) do { let regex = try NSRegularExpression(pattern: "\(uploadCount)", options: []) let range = NSRange(location:0, length: text.count) regex.enumerateMatches(in: text, options: [], range: range) { (result: NSTextCheckingResult?, _, _) in if let r = result { let subStringRange = r.range(at: 0) mutableAttributedString.addAttribute( NSAttributedString.Key.foregroundColor, value: UIColor.colorWithRGB(0xeb2c43), range: subStringRange ) desc1_lbl.attributedText = mutableAttributedString } } } catch { } ``` ## Ref. [How to change the color of a specific word in ios](https://stackoverflow.com/a/25911039) ###### tags: `UI`