将字符串属性添加到动态标签

时间:2015-10-11 09:41:52

标签: swift nsstring nsmutableattributedstring

我试图在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()允许"高分"保持绿色,但分数不会更新。

1 个答案:

答案 0 :(得分:0)

此代码:

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
}

修改

我不认为您可以设置属性文本,然后不断更新并保留格式(我可能在这里错了),我认为当您需要更新文本时,您必须设置attribtedText每次都有价值,我做了一个带标签和按钮的快速测试应用程序,每次按钮都会增加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
    }
}