ios - How to use subscript and superscript in Swift -


i want uilabel display text in following manner 6.022*1023. fumctions swift have subscript , superscript?

most of answers+examples in objc, how 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: [nsfontattributename:font!]) attstring.setattributes([nsfontattributename:fontsuper!,nsbaselineoffsetattributename:10], range: nsrange(location:8,length:2)) labelvarname.attributedtext = attstring; 

this gives me:

superscript example

in more detailed explanation:

  1. get uifont want both default , superscript style, superscript must smaller.
  2. create nsmutableattributedstring full string , default font.
  3. add attribute characters want change (nsrange), smaller/subscript uifont, , nsbaselineoffsetattributename value amount want offset vertically.
  4. assign uilabel

hopefully helps other swift devs needed well.


Comments