2

Is it possible to have different .link colors in NSAttributedString?

for example i have

let style : [NSAttributedString.Key: Any] = [
            .link: "https://www.test.com",
            NSAttributedString.Key.foregroundColor: UIColor.red,
            NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
            NSAttributedString.Key.strikethroughColor: UIColor.red
        ]
let style2 : [NSAttributedString.Key: Any] = [
            .link: "https://www.anotherTest.com",
            NSAttributedString.Key.foregroundColor: UIColor.green,
            NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
            NSAttributedString.Key.strikethroughColor: UIColor.green
        ]

and applied in a range

I tried

view.linkTextAttributes = style

but there is no range option and it is applied to all .links

i also tried to overwrite foregroundColor

textStorage.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: range)

Any ideas?

Thanks

de.
  • 7,068
  • 3
  • 40
  • 69
Stefano Vet
  • 567
  • 1
  • 7
  • 18
  • This might help: https://stackoverflow.com/a/32222702/4490923 – Asteroid Jul 29 '22 at 19:12
  • 1
    @Asteroid linkAttributes is added to all links present. Cannot be added to a range – Stefano Vet Jul 29 '22 at 19:18
  • You might need to override the methods in the CoreText part, or maybe with NSLayoutManager, on the drawing point https://developer.apple.com/documentation/uikit/nslayoutmanager – Larme Jul 29 '22 at 20:49

0 Answers0