0

I am trying to set the colour of a static part of the text in a UILabel while the dynamic part turns into the highscore. There seems to be conflict between the statement that sets the colour and the statement that sets the highscore.

These lines in viewDidLoad() make the "Highscore" part green.

var myString:NSString = "Highscore\n0"  
var myMutableString = NSMutableAttributedString()

myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:9)) 
labelHighscore.attributedText = myMutableString

While this line in another method formats the label to update the score (the 0 part) but doesn't keep the green.

labelHighscore.text = NSString(format: “Highscore\n%i", Highscore) as String

Calling viewDidLoad() after this line allows "Highscore" to stay green but then the score doesn't update.

Larme
  • 24,190
  • 6
  • 51
  • 81
Tim
  • 583
  • 1
  • 6
  • 18

1 Answers1

0

This code:

let highScore = 209

let myString = NSString(format: "Highscore\n%i", highScore)

var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:9))

Works perfectly fine for me, I'm not sure what you mean by "both" above.

if you call some code like: label.text = "Highscore\n0" this will override the attributed string, so anytime that you update the value of highScore, you'd need to set the attributed string again. there is a few ways you can make this easier, for example you could create a function that takes a score and returns the attributed, formed string. like this:

func getAttributedString(score:Int) -> NSMutableAttributedString {
    let myString = NSString(format: "Highscore\n%i", score)
    let myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!])
    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:9))

    return myMutableString
}

and then in viewDidLoad you can set it's default value:

labelHighscore.attributedText = getAttributedString(0)

and then whenever the score is updated, you can update the label:

let newScore = currentScore + 50
labelHighscore.attributedText = getAttributedString(newScore)

You could even get the function to set the attributed text of the label for you

func updateScoreLabel(score:Int) -> Void {
    let myString = NSString(format: "Highscore\n%i", score)
    let myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!])
    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:9))

    labelHighscore.attributedText = myMutableString
}

EDIT:

I don't think you can just set the attributed text and then keep updating it and keep the formatting (I could be wrong here), I think that when you need to update the text you have to set the attribtedText value each time, I done a quick test app with a label and a button, the button adds 100 to the score each time. When I tried .text = "Highscore: (highScore)" the formatting was lost, code example below.

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var button: UIButton!

    var highScore = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        updateScoreLabel()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPressed(sender: UIButton) {
        highScore += 100
        updateScoreLabel()
    }

    func updateScoreLabel() -> Void {

        let myString = NSString(format: "Highscore\n%i", highScore)
        let myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!])
        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:9))

        label.attributedText = myMutableString
    }
}
Scriptable
  • 19,402
  • 5
  • 56
  • 72