Avatar ·

Como criar uma string atribuída (NSAttributedString) em Swift?

Olá a todos! Sou novo em Swift e estou tentando fazer texto com formatação diferente em UILabel. Por exemplo, uma palavra em negrito, outra em vermelho e uma terceira sublinhada. Eu sei que existe NSAttributedString para isso, mas não consigo entender como usá-lo. Tentei escrever este código, mas não funciona: ```swift let label = UILabel() label.text = "Olá, mundo!" label.textColor = UIColor.red // quero apenas parte do texto em vermelho ``` Depois tentei fazer com NSAttributedString, mas me confundi na sintaxe. Aqui está o que escrevi: ```swift let attributedString = NSAttributedString(string: "Olá, mundo!") attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 7)) label.attributedText = attributedString ``` Mas o Xcode mostra um erro: "Cannot use mutating member on immutable value: 'attributedString' is a 'let' constant". Não entendo qual é o problema. Como criar e aplicar corretamente uma string atribuída? Preciso