4

According to Implementing Dark Mode on iOS we need to set the foregroundColor attribute to the new label color. How is this done with interface builder?

I tried using the "Text Color" option and setting the color to Developer->labelColor, but that did not work.

EDIT: As this is currently not possible, I used this workaround:

override func viewDidLoad() {
    // Support Dark Mode
    if #available(iOS 13.0, *) {
        let attributes = NSMutableAttributedString(attributedString: textView.attributedText!)
        attributes.addAttribute(.foregroundColor, value: UIColor.label, range: NSRange(location: 0, length: attributes.mutableString.length))
        textView.attributedText = attributes
    }
}
Josh
  • 504
  • 3
  • 14

4 Answers4

8

You can't do this in Interface Builder. You'll have to set the .foregroundColor attribute in code.

let mas = NSMutableAttributedString(string:s, attributes:[
    .font:UIFont(name:"GillSans", size:15)!,
    .foregroundColor:UIColor.label,
]
shim
  • 9,289
  • 12
  • 69
  • 108
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You could file a bug report with Apple, but it's not worth it in my opinion. Trying to configure an attributed string in Interface Builder has always been pretty much futile. – matt Sep 22 '19 at 18:21
4

Not sure about the attributes but I could set the color of attributed text to one of dark mode supported ones most easily with:

textView.textColor = UIColor.label
shim
  • 9,289
  • 12
  • 69
  • 108
juissi
  • 91
  • 6
2

I found a simple workaround: if you change the UILabel to be "Plain" text you can specify a Dark Mode supported color like "Label Color" then you can change the text to be "Attributed". Now it is using the Attributed Text attributes but it is still using the color you specified before. And if you don't change the color manually it will keep this color. You can still change the font sizes and weights.

Edit: I noticed that sometimes when you change the font size in IB and Xcode will change the color for you... completely ruining the automatic color change for Dark Mode. You can just discard these changes: storyboard diff

jangelsb
  • 171
  • 1
  • 5
1
static let textColor = UIColor { traitCollection in
    if traitCollection.userInterfaceStyle == .dark {
        return .white
    } else {
        return .black
    }
}

let attributedText: [NSAttributedString.Key: Any] = [
        .font: font,
        .foregroundColor: UIColor.textColor
    ]