1

with iOS 13 dark mode, I use UIColor.link for two cases:

  • foregroundColor color for NSAttributedString
  • tint color for icons

problem: though I use UIColor.link in both cases, they render as different colors. The color rendered for NSAttributedString is darker and I find this color really hard to read over a black background.

code for NSAttributedString

if #available(iOS 13.0, *) {
    linkAttributes[.foregroundColor] = UIColor.link
} else {
    linkAttributes[.foregroundColor] = UIColor.blue
}
// .font is the only other attribute I set
let attStr = NSAttributedString(string: cardName, attributes: linkAttributes)

I use this attributed string in a UIButton subclass that I instantiate programmatically:

class ConditionButton: UIButton {
    required init() {
        super.init(frame: .zero)
        configureOnce()
    }

    func configureOnce() {
        self.titleLabel?.numberOfLines = 0
        self.titleLabel?.lineBreakMode = .byWordWrapping
        self.titleLabel?.font = labelFont
        self.contentHorizontalAlignment = .left
    }

    // some more code that does self.setAttributedTitle(attributedString, for: .normal)
}

The text at the top is a hard to read label of a subclass of UIButton with NSAttributedString, at the bottom, there is an easy to read UIButton with an image and tintColor = UIColor.link

enter image description here

Why is that and how can I get the same readable color for NSAttributedString?

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89

1 Answers1

1

This is a combination of...

My mistake:

When I asked the question, I did not notice, that I had the following line in my code:

linkAttributes[.link] = url

I don't need this attribute any more, since I us a button now.

Bug in NSAttributedString:

When using

linkAttributes[.link] = url

in an NSAttributedString, Apple overrides the color with a dark blue that was ok until iOS 12.

It appears, Apple has forgotten to use UIColor.link for links in NSAttributedString.

So, everybody who uses the .link attribute in dark mode will produce this unreadable dark blue color.

I'll file a radar after writing this answer.

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89