Change Text link color swiftUI
Asked Answered
M

4

20

I have the folowing Text() that takes markdown to show a link

Text(.init("[Link Example](https://www.google.es/)"))

Example of my text

Is there a way of changing the default color set to the link?

Mitrailleuse answered 3/12, 2021 at 12:50 Comment(0)
V
32

It is possible to use accent color, like

Text(.init("[Link Example](https://www.google.es/)"))
    .accentColor(.red)
Veldaveleda answered 3/12, 2021 at 12:54 Comment(1)
doesn't work for meSinfonietta
A
20

You can achieve that with .tint(_:) as accentColor(_:) will soon be deprecated according to the documentation.

Text("[Link Example](https://www.google.es/)")
  .tint(Color.red)
Adversity answered 26/6, 2022 at 0:4 Comment(2)
doesn't work tooSinfonietta
The best way would be to use an AttributedString in Text, and define the foregroundColor on the range of the text where the link is added. Refer to this questionJustinn
W
1

Another solution using Link (iOS 14+):

if let url = URL(string: "https://www.google.es/" {
   Link(destination: url) {
      Text("Link Example")
         .foregroundColor(Color(.label))
   }
}
Waterbuck answered 9/6, 2024 at 11:57 Comment(0)
E
0

Link color is the default accent color of the app which is blue.

You can change this by setting a custom accent color:

  1. Add a color set in your Assets.xcassets

  2. In target -> Build settings, search for "Global Accent Color Name"

  3. Double click on it and set the color name aded in step 1

Thats it..!

Euphroe answered 9/10, 2023 at 14:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.