如何在UILabel中设置部分文本的位置

时间:2016-02-15 14:46:27

标签: swift text uilabel nsattributedstring nsmutableattributedstring

我需要创建一些NSMutableAttributedString,并在UILabel中设置文本位置,如何在Swift中执行此操作? 结果应该是这样的 (x + y)³而是³一些文字 我写了一些代码

    let stringTest = "(x+y)123"
    let myRange = NSRange(location: 5, length: 3)
    let myAttributeMainText = [ NSFontAttributeName: UIFont(name: "Arial", size: 18.0)! ]
    let myAttributeDegreeText = [ NSFontAttributeName: UIFont(name: "Arial", size: 10.0)! ]
    let myString = NSMutableAttributedString(string: stringTest, attributes: myAttributeMainText )
    myString.addAttributes(myAttributeDegreeText, range: myRange)
    testLabel.attributedText = myString

请帮我找到一种方法来提高UILabel顶部的最后3位数字

1 个答案:

答案 0 :(得分:1)

您需要使用NSBaselineOffsetAttributeName来使数字显示为指数,例如:

let myAttributeDegreeText = [NSBaselineOffsetAttributeName: 6, NSFontAttributeName: UIFont(name: "Arial", size: 10.0)!]

enter image description here