0

I have two for loops in my code at the moment with the specific aim of them appending the same string to the same array but in different colors based on whether they are a match or not.

if isMatch {
    secondBody.node?.run(colorTransition(fromColor: .init(customName: .brandWhite),
                                         toColor: .init(customName: .brandGreen)))
    
    for lyric in lyricsArray {
        let green = UIColor.init(customName: .brandGreen)
        let attributedStringColor = [NSAttributedString.Key.foregroundColor : green]
        let attributedString = NSAttributedString(string: lyric!, attributes: attributedStringColor)
        let lyricColor = attributedString.string
        
        array.append(lyricColor)
        defaults.set(array, forKey: "SavedLyrics")
        print(array.joined(separator: " "))
    }
} else {
    secondBody.node?.run(colorTransition(fromColor: .init(customName: .brandWhite),
                                         toColor: .init(customName: .brandRed)))
    
    for lyric in lyricsArray {
        let red = UIColor.init(customName: .brandRed)
        let attributedStringColor = [NSAttributedString.Key.foregroundColor : red]
        let attributedString = NSAttributedString(string: lyric!, attributes: attributedStringColor)
        let lyricColor = attributedString.string
        
        array.append(lyricColor)
        defaults.set(array, forKey: "SavedLyrics")
        print(array.joined(separator: " "))
    }
}

In a separate view I call the userdefaults like so:

let defaults = UserDefaults.standard
let myArray = defaults.stringArray(forKey: "SavedLyrics")
songLyricResults.text = myArray?.joined(separator: " ")

This will all work as I want it to. The only thing that isn't working is getting the textView songLyricResults to display the text in the color I have assigned in the for loop. From reading around I can see that I potentially have to convert my attributedString to a data type and reference it in that way but i'm a bit lost. Any help would be appreciated. Feel like i'm close to the desired result i'm just missing something.

trndjc
  • 11,654
  • 3
  • 38
  • 51
Joshua Browne
  • 185
  • 2
  • 13

1 Answers1

1
  1. What you ultimately add to the array is a string and not an attributed string with let lyricColor = attributedString.string. You need to add the attributed string itself, not just its string component (which is void of styling). To render the attributed string in the text view, make sure to add it to the songLyricResults.attributedText property, and not the text property.

  2. Refer to this to learn how to save an attributed string in User Defaults: How to use store and use an NSMutableAttributedString in NSUserDefaults

  3. Are you sure you want to use User Defaults to store this information? It doesn't seem like the best use case to me. Consider saving this information on disk in the File Manager, perhaps in the document directory, or with Core Data (but that has a bunch of overhead I personally avoid).

trndjc
  • 11,654
  • 3
  • 38
  • 51