【问题标题】:How to set 1st, 2nd and 3rd letters above the digit in swift [duplicate]如何在swift中设置数字上方的第一个,第二个和第三个字母[重复]
【发布时间】:2017-06-10 14:27:00
【问题描述】:

我想在UILabel 中设置像 12th 3rd 1st 这样的文本。但是数字上方应该出现2个字母。请帮帮我。 谢谢

【问题讨论】:

  • 能否请您添加一个您想要的示例?
  • 它类似于 10^3
  • @MoeAbdul-Hameed 请看我添加的图片

标签: ios swift3 uilabel


【解决方案1】:

这个函数应该完全符合您的要求:

func prepareLabel(label: UILabel, string: String, superScript: String) {

    let font = UIFont(name: "Helvetica", size:20)
    let fontSuper = UIFont(name: "Helvetica", size:10)

    let attributedString = NSMutableAttributedString(string: string + superScript, attributes: [NSFontAttributeName:font!])
    attributedString.setAttributes([NSFontAttributeName:fontSuper!, NSBaselineOffsetAttributeName:10], range: NSRange(location:string.characters.count, length:superScript.characters.count))
    label.attributedText = attributedString
}

用法:

prepareLabel(label: self.label, string: "15", superScript: "th")

此功能的灵感来自answer,请查看它以获取更多信息。

祝你好运!

【讨论】:

    猜你喜欢
    • 2022-08-23
    • 2022-11-04
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多