Ios 在Swift中,如何更改字符串部分的属性?
我试图用一些文字显示分数。分数显示在句子的中间,我希望字体比其他文本更大。 我的代码如下:Ios 在Swift中,如何更改字符串部分的属性?,ios,swift,nsattributedstring,Ios,Swift,Nsattributedstring,我试图用一些文字显示分数。分数显示在句子的中间,我希望字体比其他文本更大。 我的代码如下: let fontSizeAttribute = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 43)] let myString = String(describing: Int(finalScore!.rounded(toPlaces: 0))) let attributedString = NSAttributedString(strin
let fontSizeAttribute = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 43)]
let myString = String(describing: Int(finalScore!.rounded(toPlaces: 0)))
let attributedString = NSAttributedString(string: myString, attributes: fontSizeAttribute)
scoreLabel.text = "Your score is \(attributedString)%, which is much higher than most people."
我看不出这个实现有什么问题,但当我运行它时,它会说,“您的分数是9{NSFont=“UITCFont:0x7f815…”
我觉得我在做一些愚蠢的事情,但我不知道这是什么。任何帮助都将不胜感激!请检查:
let fontSizeAttribute = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 43)]
let myString = String(describing: Int(finalScore!.rounded(toPlaces: 0)))
let partOne = NSMutableAttributedString(string: "Your ")
let partTwo = NSMutableAttributedString(string: myString, attributes: fontSizeAttribute)
let partThree = NSMutableAttributedString(string: "%, which is much higher than most people.")
let attributedString = NSMutableAttributedString()
attributedString.append(partOne)
attributedString.append(partTwo)
attributedString.append(partThree)
scoreLabel.attributedText = attributedString
你可以在这里找到解决方案。你应该设置标签attributedtext而不是late text属性哇。我尝试了上面发布的链接,但没有解决问题。我花了几个小时试图解决这个问题,最终发现了.attributedtext函数。所以我最终完全按照你说的做了是的,然后当我回去回答我自己的问题时,我找到了你的答案。无论如何,谢谢!我接受了答案。