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:
in more detailed explanation:
- get
uifont
want both default , superscript style, superscript must smaller. - create
nsmutableattributedstring
full string , default font. - add attribute characters want change (
nsrange
), smaller/subscriptuifont
, ,nsbaselineoffsetattributename
value amount want offset vertically. - assign
uilabel
hopefully helps other swift devs needed well.
Comments
Post a Comment