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

关于ios - 在一个属性字符串中使用多种样式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48458028/

10-10 22:21
查看更多