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
Asked
Active
Viewed 600 times
1

Randi
- 639
- 2
- 6
- 23
-
1May you please add an example of what you want? – Mo Abdul-Hameed Jun 10 '17 at 14:30
-
its something like 10^3 – Randi Jun 10 '17 at 14:38
-
@MoeAbdul-Hameed pls see the image I added – Randi Jun 10 '17 at 14:59
1 Answers
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