UITextView erases foreground color of attributedText

This issue is driving me crazy. I load an NSAttributedString in UITextView and within moments after loading the foregroundColor attribute of text is erased(i.e becomes white) without me doing anything. Here is the code and NSLog dump. How do I debug this I wonder?

class ScriptEditingView: UITextView, UITextViewDelegate {

      var defaultFont = UIFont.preferredFont(forTextStyle: .body)
      var defaultTextColor = UIColor.white

       private func commonInit() {
         self.font = UIFont.preferredFont(forTextStyle: .body)
         self.allowsEditingTextAttributes = true
         self.textColor = defaultTextColor
         self.backgroundColor = UIColor.black
         self.isOpaque = true
         self.isEditable = true
         self.isSelectable = true
         self.dataDetectorTypes = []
         self.showsHorizontalScrollIndicator = false
      }
  }

And then in my ViewController that contains the UITextView, I have this code:

 textView = ScriptEditingView(frame: newTextViewRect, textContainer: nil)
    textView.delegate = self
    view.addSubview(textView)
    textView.allowsEditingTextAttributes = true
    
    let guide = view.safeAreaLayoutGuide
    
    // 5
    textView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      textView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
      textView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
      textView.topAnchor.constraint(equalTo: view.topAnchor),
      textView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])
    
    textView.attributedText = attributedString
    
    NSLog("Attributed now")
    dumpAttributesOfText()
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        NSLog("Attributes after 1 sec")
        self.dumpAttributesOfText()
    }

And here is code to dump attributes of text:

private func dumpAttributesOfText() {
    textView.attributedText?.enumerateAttributes(in: NSRange(location: 0, length: textView.attributedText!.length), options: .longestEffectiveRangeNotRequired, using: { dictionary, range, stop in
           NSLog(" range \(range)")
           
           if let font = dictionary[.font] as? UIFont {
              NSLog("Font at range \(range) - \(font.fontName), \(font.pointSize)")
           }
           
           if let foregroundColor = dictionary[.foregroundColor] as? UIColor {
               NSLog("Foregroundcolor \(foregroundColor) at range \(range)")
           }
           
           if let underline = dictionary[.underlineStyle] as? Int {
               NSLog("Underline \(underline) at range \(range)")
           }
    })
}


The logs show this:

 2022-07-02 13:16:02.841199+0400 MyApp[12054:922491] Attributed now
    2022-07-02 13:16:02.841370+0400 MyApp[12054:922491]  range {0, 14}
    2022-07-02 13:16:02.841486+0400 MyApp[12054:922491] Font at range {0, 14} - HelveticaNeue, 30.0
    2022-07-02 13:16:02.841586+0400 MyApp[12054:922491] Foregroundcolor UIExtendedGrayColorSpace 1 1 at range {0, 14}
    2022-07-02 13:16:02.841681+0400 MyApp[12054:922491]  range {14, 6}
    2022-07-02 13:16:02.841770+0400 MyApp[12054:922491] Font at range {14, 6} - HelveticaNeue, 30.0
    2022-07-02 13:16:02.841855+0400 MyApp[12054:922491] Foregroundcolor kCGColorSpaceModelRGB 0.96863 0.80784 0.27451 1  at range {14, 6}
   


    2022-07-02 13:16:03.934816+0400 MyApp[12054:922491] Attributes after 1 sec
    2022-07-02 13:16:03.935087+0400 MyApp[12054:922491]  range {0, 20}
    2022-07-02 13:16:03.935183+0400 MyApp[12054:922491] Font at range {0, 20} - HelveticaNeue, 30.0
    2022-07-02 13:16:03.935255+0400 MyApp[12054:922491] Foregroundcolor UIExtendedGrayColorSpace 1 1 at range {0, 20}

Have you tried to re-access the attributedText without using the DispatchQueue? I have some similar problems with UITextView.

UITextView erases foreground color of attributedText
 
 
Q