AttributedString: formato, enlaces y VoiceOver en SwiftUI AttributedString: formatting, links, and VoiceOver in SwiftUI
Cómo dar formato a un texto con AttributedString, gestionar enlaces con OpenURLAction y hacer que VoiceOver los reconozca como enlaces. How to format text with AttributedString, handle links with OpenURLAction, and make VoiceOver recognize them as links.
Un párrafo con una negrita, una cursiva, un fragmento de código y dos enlaces parece un problema resuelto. Hasta que toca que VoiceOver lo lea como lo que es: texto con énfasis y enlaces, no una sola frase plana.
El problema no es solo pintar el texto. Un enlace tiene que seguir siéndolo cuando VoiceOver lo descubre, cuando cambia el tamaño de la letra o cuando el párrafo se combina con otros elementos.
AttributedString no elimina toda la complejidad, pero junta las piezas en un solo valor. Y, sobre todo, permite que el formato tenga significado además de apariencia.
Dos capas: cómo se ve y qué es
El error clásico es tratar un enlace como texto azul con subrayado. Para los ojos, eso es un enlace. Para VoiceOver, no. Lo que convierte un fragmento en un enlace es el atributo .link, no el color.
Lo mismo pasa con la negrita y la cursiva. Puedes pintar una palabra con una fuente concreta o puedes marcar que esa palabra está enfatizada. La primera opción cambia la apariencia; la segunda también puede cambiar cómo se interpreta el texto.
import SwiftUI
struct ArticleText: View {
private var content: AttributedString {
var text = AttributedString("AttributedString permite mezclar formato, enlaces y semántica en un solo valor.")
if let range = text.range(of: "formato") {
text[range].inlinePresentationIntent = .stronglyEmphasized
}
if let range = text.range(of: "enlaces") {
text[range].link = URL(string: "https://developer.apple.com/documentation/foundation/attributedstring")
text[range].underlineStyle = .single
text[range].foregroundColor = .accentColor
}
return text
}
var body: some View {
Text(content)
.font(.body)
.tint(.accentColor)
}
}
#Preview {
ArticleText()
}
inlinePresentationIntent expresa la intención —énfasis fuerte, énfasis, código— y SwiftUI decide cómo representarla. Si necesitas una fuente concreta, puedes usar .font, pero para texto de lectura conviene partir de estilos semánticos como .body o .headline para que Dynamic Type siga funcionando.
El atributo que no es negociable es .link. El subrayado y el color ayudan a verlo, pero no lo convierten en un enlace. Si quitas .link y dejas solo el color azul, tendrás texto bonito y ningún enlace.
Markdown como punto de partida
Si el texto ya viene en Markdown, no hace falta recorrer rangos a mano:
import Foundation
func makeMarkdown() throws -> AttributedString {
let markdown = "**Importante:** lee la [guía de AttributedString](https://developer.apple.com/documentation/foundation/attributedstring)."
let options = AttributedString.MarkdownParsingOptions(
interpretedSyntax: .inlineOnlyPreservingWhitespace
)
return try AttributedString(markdown: markdown, options: options)
}
inlineOnlyPreservingWhitespace interpreta el Markdown como texto en línea y conserva los saltos de línea. Es útil para párrafos que vienen de una API, una nota o un campo de texto.
Text también acepta Markdown directamente en un string. La diferencia es que AttributedString te deja el valor en la mano: puedes añadir atributos, cambiar un enlace, combinar varios fragmentos o pasarlo a otra capa antes de renderizarlo.
Enlaces que se comportan como enlaces
En SwiftUI, un Text con un AttributedString que contiene .link ya es interactivo. No necesitas un gesto sobre el texto ni calcular qué palabra se ha pulsado.
Si quieres decidir qué ocurre con algunos enlaces, usa OpenURLAction:
import SwiftUI
struct LinkHandlingView: View {
let text: AttributedString
var body: some View {
Text(text)
.environment(\.openURL, OpenURLAction { url in
guard url.scheme == "myapp" else {
return .systemAction
}
// Abre una pantalla interna o registra el evento.
return .handled
})
}
}
.systemActiondeja que el sistema abra el enlace: Safari, Mail, la app que corresponda..handledindica que ya lo has gestionado..discardedlo ignora.
Para acciones internas, un esquema propio como myapp://glossary/attributedstring es suficiente. Para contenido web normal, deja que el sistema haga su trabajo. No conviertas todos los enlaces en acciones internas solo porque puedes.
VoiceOver: el enlace tiene que existir en el árbol
Cuando Text encuentra un fragmento con .link, lo expone como un enlace. VoiceOver lo anuncia como tal y lo añade al rotor de enlaces. No hace falta añadir .accessibilityAddTraits(.isLink) al párrafo entero; de hecho, puede ser contraproducente.
Hay tres formas bastante habituales de romperlo:
- Pintar el texto de azul y subrayarlo sin
.link. - Usar
.onTapGesturesobre elTextpara abrir una pantalla. - Aplicar
.accessibilityElement(children: .combine)o un.accessibilityLabelal párrafo completo.
Las tres convierten el párrafo en una sola parada. Visualmente sigue pareciendo un enlace; para VoiceOver, ya no lo es.
El texto del enlace también importa. «Aquí» o «más información» no dicen nada fuera de contexto. «Guía de AttributedString» sí. VoiceOver permite recorrer los enlaces con el rotor; si todos se llaman «aquí», el rotor es inútil.
Prueba con el rotor de enlaces, no solo leyendo el párrafo de arriba abajo. En el simulador, activa VoiceOver o usa Accessibility Inspector. Comprueba que cada enlace aparece en el rotor, que el foco no se queda atrapado y que el orden coincide con el texto.
Accesibilidad: cumplir y, sobre todo, no obligar a reaprender
La accesibilidad tampoco es solo una decisión de producto. La Ley Europea de Accesibilidad (European Accessibility Act, EAA), la Directiva (UE) 2019/882, se aplica desde el 28 de junio de 2025. Si tu app entra en su ámbito —por ejemplo, porque ofrece o da acceso a comercio electrónico, banca, transporte de pasajeros, comunicaciones electrónicas o contenidos audiovisuales—, la accesibilidad pasa a ser un requisito legal, no un extra. Cada Estado miembro debe establecer sanciones efectivas, proporcionadas y disuasorias; la cuantía concreta depende de su transposición nacional. No todas las apps están cubiertas y hay exenciones, así que conviene revisar el ámbito antes de convertir esto en una promesa legal.
Pero el argumento práctico es anterior a la norma. Una persona que ya usa VoiceOver en su iPhone no debería aprender un modelo de interacción nuevo para tu app. Si los enlaces se comportan como enlaces, los botones como botones y los encabezados como encabezados, esa persona llega con parte del trabajo hecho. La app se siente predecible, el aprendizaje inicial es más corto y el soporte recibe menos preguntas del tipo «¿por qué esto no se puede activar?».
Hay un extra: una interfaz accesible también es una interfaz más legible para una máquina. Apple Intelligence y los agentes que interactúan con apps necesitan semántica: etiquetas, roles, valores y acciones. Si VoiceOver puede manejar tu app, es más probable que un agente de Apple Intelligence también lo haga. No es una garantía, pero una app que se explica bien a VoiceOver también se explica mejor a cualquier sistema que intente entenderla.
Eso no significa que la accesibilidad sea gratis. Significa que gran parte del valor está en usar las convenciones que el sistema ya ofrece, en lugar de inventar gestos o etiquetas que solo funcionan en tu pantalla. El rotor de enlaces y el foco son infraestructura compartida. Cuanto más se respetan, menos tiene que aprender quien ya conoce VoiceOver.
La accesibilidad también reduce el riesgo de que una función exista solo para quien ve la pantalla. Un enlace que solo es azul, un botón que solo responde a un tap o un estado que solo se comunica por color son deuda de accesibilidad.
La decisión práctica
| Necesito | Uso |
|---|---|
| Varios enlaces dentro de un párrafo en SwiftUI | AttributedString + Text |
| Decidir qué hace un enlace | OpenURLAction |
| Un enlace suelto y aislado | Link |
| Que VoiceOver lo reconozca | atributo .link, no color ni subrayado |
El formato no es decoración. Un enlace es una promesa de navegación; si solo lo parece, la promesa se rompe para quien no lo ve. AttributedString no es solo una API más cómoda: es una forma de que el texto lleve su significado consigo.
Referencias
A paragraph with bold, italic, a code fragment, and two links sounds like a solved problem. Then VoiceOver has to read it as what it is: text with emphasis and links, not one flat sentence.
The problem is not just painting the text. A link has to remain a link when VoiceOver discovers it, when the text size changes, or when the paragraph is combined with other elements.
AttributedString does not remove every sharp edge, but it brings the pieces into one value. More importantly, it lets formatting carry meaning, not just appearance.
Two layers: how it looks and what it is
The classic mistake is treating a link as blue, underlined text. To your eyes, that is a link. To VoiceOver, it is not. What turns a run into a link is the .link attribute, not the color.
The same applies to bold and italic. You can paint a word with a specific font, or you can mark that the word is emphasized. The first changes appearance; the second can also change how the text is interpreted.
import SwiftUI
struct ArticleText: View {
private var content: AttributedString {
var text = AttributedString("AttributedString can mix formatting, links, and semantics in a single value.")
if let range = text.range(of: "formatting") {
text[range].inlinePresentationIntent = .stronglyEmphasized
}
if let range = text.range(of: "links") {
text[range].link = URL(string: "https://developer.apple.com/documentation/foundation/attributedstring")
text[range].underlineStyle = .single
text[range].foregroundColor = .accentColor
}
return text
}
var body: some View {
Text(content)
.font(.body)
.tint(.accentColor)
}
}
#Preview {
ArticleText()
}
inlinePresentationIntent expresses intent — strong emphasis, emphasis, code — and SwiftUI decides how to render it. If you need a specific font, you can use .font, but for reading text it is better to start from semantic styles such as .body or .headline so Dynamic Type keeps working.
The non-negotiable attribute is .link. Underline and color help people see it, but they do not make it a link. Remove .link and keep the blue color, and you have pretty text with no link at all.
Markdown as a starting point
If the text already arrives as Markdown, you do not need to walk ranges by hand:
import Foundation
func makeMarkdown() throws -> AttributedString {
let markdown = "**Important:** read the [AttributedString guide](https://developer.apple.com/documentation/foundation/attributedstring)."
let options = AttributedString.MarkdownParsingOptions(
interpretedSyntax: .inlineOnlyPreservingWhitespace
)
return try AttributedString(markdown: markdown, options: options)
}
inlineOnlyPreservingWhitespace parses Markdown as inline text and preserves line breaks. It is useful for paragraphs that come from an API, a note, or a text field.
Text also accepts Markdown directly in a string. The difference is that AttributedString leaves the value in your hands: you can add attributes, replace a link, combine fragments, or pass it to another layer before rendering it.
Links that behave like links
In SwiftUI, a Text backed by an AttributedString with .link is already interactive. You do not need a gesture on the text or code that works out which word was tapped.
If you want to decide what happens to some links, use OpenURLAction:
import SwiftUI
struct LinkHandlingView: View {
let text: AttributedString
var body: some View {
Text(text)
.environment(\.openURL, OpenURLAction { url in
guard url.scheme == "myapp" else {
return .systemAction
}
// Open an internal screen or record the event.
return .handled
})
}
}
.systemActionlets the system open the link: Safari, Mail, or the matching app..handledmeans you have already handled it..discardedignores it.
For internal actions, a custom scheme such as myapp://glossary/attributedstring is enough. For ordinary web content, let the system do its job. Do not turn every link into an internal action just because you can.
VoiceOver: the link has to exist in the tree
When Text finds a run with .link, it exposes it as a link. VoiceOver announces it as a link and adds it to the links rotor. You do not need to add .accessibilityAddTraits(.isLink) to the whole paragraph; in fact, it can backfire.
There are three common ways to break it:
- Paint the text blue and underline it without
.link. - Use
.onTapGestureon theTextto open a screen. - Apply
.accessibilityElement(children: .combine)or an.accessibilityLabelto the entire paragraph.
All three collapse the paragraph into a single stop. It still looks like a link; to VoiceOver, it is no longer one.
Link text matters too. “Here” or “more information” says nothing out of context. “AttributedString guide” does. VoiceOver lets people move through links with the rotor; if every link is called “here”, the rotor is useless.
Test with the links rotor, not just by reading the paragraph from top to bottom. In the simulator, turn on VoiceOver or use Accessibility Inspector. Check that each link appears in the rotor, that focus does not get trapped, and that the order matches the text.
Accessibility: comply, but do not force people to relearn
Accessibility is not only a product decision. The European Accessibility Act (EAA), Directive (EU) 2019/882, has applied since 28 June 2025. If your app falls within its scope — for example, because it provides or gives access to e-commerce, banking, passenger transport, electronic communications, or audiovisual media — accessibility becomes a legal requirement, not an extra. Each Member State must set effective, proportionate, and dissuasive penalties; the actual amount depends on its national transposition. Not every app is covered, and there are exemptions, so check the scope before turning this into a legal promise.
The practical argument comes before the law, though. Someone who already uses VoiceOver on their iPhone should not have to learn a new interaction model for your app. If links behave like links, buttons like buttons, and headings like headings, that person arrives with part of the work already done. The app feels predictable, initial learning is shorter, and support gets fewer “why can’t I activate this?” questions.
One more thing: an accessible interface is also a more machine-readable interface. Apple Intelligence and agents that interact with apps need semantics: labels, roles, values, and actions. If VoiceOver can handle your app, an Apple Intelligence agent is more likely to do the same. It is not a guarantee, but an app that explains itself well to VoiceOver also explains itself better to any system trying to understand it.
That does not mean accessibility is free. It means much of the value comes from using the conventions the system already provides instead of inventing gestures or labels that only work on your screen. The links rotor and focus are shared infrastructure. The more you respect them, the less someone who already knows VoiceOver has to learn.
Accessibility also reduces the risk of a feature existing only for people who can see the screen. A link that is only blue, a button that only responds to a tap, or a state communicated only by color is accessibility debt.
The practical decision
| I need | I use |
|---|---|
| Several links inside a SwiftUI paragraph | AttributedString + Text |
| Decide what a link does | OpenURLAction |
| A single, isolated link | Link |
| VoiceOver to recognize it | the .link attribute, not color or underline |
Formatting is not decoration. A link is a navigation promise; if it only looks like one, the promise breaks for people who cannot see it. AttributedString is not just a more convenient API: it is a way for text to carry its meaning with it.
References
Publicado el Published on