Superscript属性不再适用于iOS 10.3

时间:2017-05-10 15:30:56

标签: ios nsattributedstring

我最近注意到,作为NSAttributedString的Superscripted文本不再适用于iOS 10.3。

有没有人注意到这个?此代码用于工作并使我的注册符号上标:

func setSuperScript(forSuperScriptString superScriptString:String, withFont font:String, atSize size:CGFloat){

    if self.text != nil {

        let mutableAttString:NSMutableAttributedString = NSMutableAttributedString(string:self.text!)

        for (i,c) in self.text!.characters.enumerated() {

            if c == Character(superScriptString) {

                let range = NSRange(location:i,length:1)

                // for each occurence of the character to superscript
                mutableAttString.addAttribute(NSFontAttributeName, value: UIFont(name: font, size: size)!, range: range)
                mutableAttString.addAttribute(kCTSuperscriptAttributeName as String, value:1, range: range)
            }
        }

        self.attributedText = mutableAttString
    }
}

此行mutableAttString.addAttribute(kCTSuperscriptAttributeName as String, value:1, range: range)适用于运行iOS 10.2的设备,但不适用于最新版本的iOS。

1 个答案:

答案 0 :(得分:0)

我有这个代码在货币标签上标以美分,并且从iOS 8到10.3.1它的效果非常好:

func formatLabelToCurrency(label: UILabel)-> UILabel {
    var decimalSeparator: String = "."
    let font:UIFont? = UIFont(name: "Helvetica", size:20)
    let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10)
    let attString:NSMutableAttributedString = NSMutableAttributedString(string: label.text!, attributes: [NSFontAttributeName:font!])
    attString.setAttributes([NSFontAttributeName:fontSuper!,NSBaselineOffsetAttributeName:10], range: NSRange(location:(label.text?.index(of: decimalSeparator))!,length:3))
    label.attributedText = attString;
    return label
}