Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use subscript and superscript in Swift?

Tags:

ios

swift

I want my UILabel to display text in following manner 6.022*1023. What functions does Swift have for subscript and superscript?

like image 465
Adam Young Avatar asked Mar 24 '15 05:03

Adam Young


People also ask

How do you add a superscript in Swift?

Create a NSMutableAttributedString with the full string and default font. Add an attribute to the characters you want to change ( NSRange ), with the smaller/subscript UIFont , and the NSBaselineOffsetAttributeName value is the amount you want to offset it vertically. Assign it to your UILabel.

How do you use subscript in Swift?

'subscript' keyword is used for defining subscripts and the user can specify single or multiple parameters with their return types. Subscripts can have read-write or read-only properties and the instances are stored and retrieved with the help of 'getter' and 'setter' properties as that of computed properties.

How do I type a superscript in Xcode?

Pressing Cmd + Ctrl + Space will open a special characters menu. Check if the "Digits — All" category is in the left-hand column. If it isn't, click the gear icon, then select this category — add it to the list.


2 Answers

Most of the answers+examples are in ObjC, but this is how to do it in Swift.

let font:UIFont? = UIFont(name: "Helvetica", size:20) let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10) let attString:NSMutableAttributedString = NSMutableAttributedString(string: "6.022*1023", attributes: [.font:font!]) attString.setAttributes([.font:fontSuper!,.baselineOffset:10], range: NSRange(location:8,length:2)) labelVarName.attributedText = attString 

This gives me:

SuperScript Example

In a more detailed explanation:

  1. Get UIFont you want for both the default and superscript style, superscript must be smaller.
  2. Create a NSMutableAttributedString with the full string and default font.
  3. Add an attribute to the characters you want to change (NSRange), with the smaller/subscript UIFont, and the NSBaselineOffsetAttributeName value is the amount you want to offset it vertically.
  4. Assign it to your UILabel

Hopefully this helps other Swift devs as I needed this as well.

like image 132
Forrest Porter Avatar answered Sep 20 '22 06:09

Forrest Porter


As a different approach, I wrote a function that takes in a string where the exponents are prepended with ^ such as 2^2•3•5^2 and returns 2²•3•5²

func exponentize(str: String) -> String {      let supers = [         "1": "\u{00B9}",         "2": "\u{00B2}",         "3": "\u{00B3}",         "4": "\u{2074}",         "5": "\u{2075}",         "6": "\u{2076}",         "7": "\u{2077}",         "8": "\u{2078}",         "9": "\u{2079}"]      var newStr = ""     var isExp = false     for (_, char) in str.characters.enumerate() {         if char == "^" {             isExp = true         } else {             if isExp {                 let key = String(char)                 if supers.keys.contains(key) {                     newStr.append(Character(supers[key]!))                 } else {                     isExp = false                     newStr.append(char)                 }             } else {                 newStr.append(char)             }         }     }     return newStr } 

It's a bit of a brute force method, but it works if you don't want to deal with attributed strings or you want your string to be independent of a font.

like image 45
Chris Avatar answered Sep 21 '22 06:09

Chris