🎨 Como Criar e Usar NSAttributedString no Desenvolvimento Apple
NSAttributedString é uma classe da framework Foundation usada para representar strings com atributos de formatação. Ela é essencial para criar textos ricos em interfaces gráficas, como títulos estilizados, mensagens com ênfase ou textos interativos.
🧱 Diferença entre NSAttributedString e NSMutableAttributedString
Classe
Característica
NSAttributedString
Imutável (não pode ser modificada)
NSMutableAttributedString
Mutável (permite adicionar ou alterar atributos)
✏️ Criando uma NSAttributedString Simples
import UIKit
let texto = "Olá, Antonio!"
let atributos: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.systemBlue,
.font: UIFont.boldSystemFont(ofSize: 20)
]
let textoAtribuido = NSAttributedString(string: texto, attributes: atributos)
🧪 Usando NSMutableAttributedString para Estilizar Partes do Texto