I want my UILabel
to display text in following manner 6.022*1023. What functions does Swift have for subscript and superscript?
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.
'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.
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.
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:
In a more detailed explanation:
UIFont
you want for both the default and superscript style, superscript must be smaller.NSMutableAttributedString
with the full string and default font.NSRange
), with the smaller/subscript UIFont
, and the NSBaselineOffsetAttributeName
value is the amount you want to offset it vertically.UILabel
Hopefully this helps other Swift devs as I needed this as well.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With