1

I want to set text like 12th 3rd 1st in a UILabel. but 2 letters should be appeared above the digit.Please help me. Thanks

UPDATE

Randi
  • 639
  • 2
  • 6
  • 23

1 Answers1

1

This function should do exactly what you are looking for:

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
}

Usage:

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

This function was inspired by this answer, take a look at it for more information.

Good luck!

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36