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.