Avatar ·

Cómo crear una cadena atribuida (NSAttributedString) en Swift?

¡Hola a todos! Soy nuevo en Swift y estoy intentando hacer texto con diferente formato en un UILabel. Por ejemplo, que una palabra esté en negrita, otra en rojo y una tercera subrayada. Sé que para esto existe NSAttributedString, pero no logro entender cómo usarlo. Intenté escribir este código, pero no funciona: ```swift let label = UILabel() label.text = "¡Hola, mundo!" label.textColor = UIColor.red // quiero solo parte del texto en rojo ``` Luego intenté hacerlo con NSAttributedString, pero me confundí con la sintaxis. Esto es lo que escribí: ```swift let attributedString = NSAttributedString(string: "¡Hola, mundo!") attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 7)) label.attributedText = attributedString ``` Pero Xcode muestra un error: "Cannot use mutating member on immutable value: 'attributedString' is a 'let' constant". No entiendo cuál es el problema. ¿Cómo crear y aplicar correctamente una cadena atribuida? Necesito