在一个属性字符串中使用多种样式

Using multiple styles in one attributed string

let myString = text1 + " (\(text2) people added)"
let myAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
cell.textLabel?.attributedText = myAttrString

如何在一个属性字符串中使用不同样式的 text1" (\(text2) people added)"

是的,你可以

    var attributedText = NSMutableAttributedString()
    let str1 = text1
    let str2 = " (\(text2) people added)"
    let attr1 = [NSAttributedStringKey.foregroundColor: UIColor.blue]
    let attr2 = [NSAttributedStringKey.foregroundColor: UIColor.red]
    attributedText.append(NSAttributedString(string: str1, attributes: attr1))
    attributedText.append(NSAttributedString(string: str2, attributes: attr2))

    cell.textLabel?.attributedText = attributedText