gpt4 book ai didi

ios - 在 Swift 中,如何更改字符串的一部分的属性?

转载 作者:行者123 更新时间:2023-11-28 15:18:43 25 4
gpt4 key购买 nike

我正在尝试用一些文本显示乐谱。分数显示在句子的中间,我希望分数的字体比文本的其余部分大。

我的代码如下:

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

关于ios - 在 Swift 中,如何更改字符串的一部分的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46396260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com