how to change font color on a [String] type?

I have an array of words thats being collected and distributed with chosenWords array and would like to have the words in this array in red font when they are being distributed back in label text. I have tried NSattributed strings but with no luck.


here is my code


class GameVC: UIViewController {

@IBOutlet weak var storyLabel: UILabel!


override func viewDidLoad() {

super.viewDidLoad()


if let Path = Bundle.main.url(forResource: "fortelling2", withExtension: "rtf") {

do {

let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: Path, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)

self.storyLabel.attributedText = attributedStringWithRtf

self.storyLabel.font = UIFont(name: "TimesNewRomanPS-BoldItalicMT",

size: 30.0)

} catch let error {

print("Got an error \(error)")

}

while chosenWords.count > 1 {

let replace = storyLabel.text?.range(of: "§§§")

storyLabel.text?.replaceSubrange(replace!, with: chosenWords.removeFirst())

}

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}

I do not see where and how you change text in red.


In addition, please use code formatter (<>) to post code, and delete supefluous blank lines, much easier to read


class GameVC: UIViewController {
  
    @IBOutlet weak var storyLabel: UILabel!
  
    override func viewDidLoad() {
        super.viewDidLoad()
   
        if let Path = Bundle.main.url(forResource: "fortelling2", withExtension: "rtf") {
            do {
                let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: Path, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
                self.storyLabel.attributedText = attributedStringWithRtf
                self.storyLabel.font = UIFont(name: "TimesNewRomanPS-BoldItalicMT", size: 30.0)
            } catch let error {
                print("Got an error \(error)")
            }
                   
            while  chosenWords.count > 1 {             
                let replace = storyLabel.text?.range(of: "§§§")
                 storyLabel.text?.replaceSubrange(replace!, with: chosenWords.removeFirst())
                }
            }
            }
  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
}

really sorry, here is the code i tried

import UIKit
import Foundation
class GameVC: UIViewController {
  
    @IBOutlet weak var storyLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
  
        if let Path = Bundle.main.url(forResource: "fortelling2", withExtension: "rtf") {
            do {
                let attributedStringWithRtf: NSAttributedString = try NSAttributedString(url: Path, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
                self.storyLabel.attributedText = attributedStringWithRtf
                self.storyLabel.font = UIFont(name: "TimesNewRomanPS-BoldItalicMT",
                                              size: 30.0)
              
            } catch let error {
                print("Got an error \(error)")
            }
          
            let color : [UIColor] = [.red]
            let textRange = chosenWords
            let str = NSMutableAttributedString(string: textRange)
            for var i in 0..<textRange.characters.count {
                let range =  NSMakeRange(i, 1)
                let newColor = color[i % color.count]
              
                str.addAttribute(NSForegroundColorAttributeName, value: newColor, range: range)
            }
               
            while  chosenWords.count > 1 {
              
                let replace = storyLabel.text?.range(of: "§§§")
                storyLabel.text?.replaceSubrange(replace!, with: chosenWords.removeFirst())
                }  
            }
            }
  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }
}

To change the text of the label, you need to set its attributedText property:

replace

storyLabel.text?.replaceSubrange(replace!, with: chosenWords.removeFirst())

with

storyLabel.attributedText?.replaceSubrange(replace!, with: chosenWords.removeFirst())


There are a few things I do not understand well in your code:


            let color : [UIColor] = [.red]
            let textRange = chosenWords
            let str = NSMutableAttributedString(string: textRange)
            for var i in 0..<textRange.characters.count {
                let range =  NSMakeRange(i, 1)
                let newColor = color[i % color.count]
      
                str.addAttribute(NSForegroundColorAttributeName, value: newColor, range: range)
            }


Line 2 : where is chosenWords defined ? How ? What is its value ?

line 4 : i is not modified, you could write for i in 0..<

line 5: color.count is 1 ; hence %1 will alwauys yeld 0 ; hence newColor = color[0] = .red, always

line 8 : which version of XCode ? In recent versions, key is foregroundColor , no more NSForegroundColorAttributeName

how to change font color on a [String] type?
 
 
Q