Swift 向动态标签添加字符串属性

Swift 向动态标签添加字符串属性,swift,nsstring,nsmutableattributedstring,Swift,Nsstring,Nsmutableattributedstring,我试图在UILabel中设置文本静态部分的颜色,而动态部分变为高分。设置颜色的语句和设置高分的语句之间似乎存在冲突 viewDidLoad()中的这些行使“Highscore”部分变成绿色 var myString:NSString = "Highscore\n0" var myMutableString = NSMutableAttributedString() myMutableString = NSMutableAttributedString(string: myString as

我试图在
UILabel
中设置文本静态部分的颜色,而动态部分变为高分。设置颜色的语句和设置高分的语句之间似乎存在冲突

viewDidLoad()
中的这些行使“Highscore”部分变成绿色

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
而另一种方法中的这一行格式化标签以更新分数(0部分),但不保持绿色

labelHighscore.text = NSString(format: “Highscore\n%i", Highscore) as String
在此行之后调用
viewDidLoad()
允许“Highscore”保持绿色,但分数不会更新。

此代码:

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))
这对我来说非常好,我不知道上面的“两者”是什么意思

如果调用某些代码,如:
label.text=“Highscore\n0”
这将覆盖属性字符串,因此在任何时候更新Highscore的值时,都需要再次设置属性字符串。有几种方法可以简化此过程,例如,可以创建一个函数,该函数接受分数并返回属性化的、格式化的字符串。像这样:

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
}
然后在viewDidLoad中,您可以设置其默认值:

labelHighscore.attributedText = getAttributedString(0)
然后,每当分数更新时,您都可以更新标签:

let newScore = currentScore + 50
labelHighscore.attributedText = getAttributedString(newScore)
您甚至可以使用函数为您设置标签的属性文本

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
}
编辑:

我不认为你可以只设置属性文本,然后不断更新它并保留格式(我可能在这里是错误的),我认为当你需要更新文本时,你必须每次设置attribtext值,我用一个标签和一个按钮做了一个快速测试应用,按钮每次都会增加100分。当我尝试时。text=“Highscore:(Highscore)”格式丢失,下面的代码示例

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
    }
}

嗨,谢谢你的回答。我已经澄清了我的问题,如果它有帮助的话。嗨,我已经更新了我的答案,我是如何在一个测试项目中完成的。