ios - Multiline UIButton autosize per line -


i want use standard uibutton text want put on uibutton has...

  1. ... multiple lines (each line different string) , ...
  2. ... each line should have different font , size ...
  3. ... automatically resizes font fit button width (not height make bit easier)

enter image description here

so, tough set (preferred) font size each line, want font-size automatically size down each separate line fits nicely in uibutton (=same behaviour uilabel autoshrink / minimum font scale).

what don't want:

i not want start adding uilabels uibutton (as subview example) or using ib put uilabels on scene , draw uibutton around (why: want standard uibutton highlighting behaviour)

what want:

a clean solution using attributed string, given width scales down font (updates attributed string guess), line-by-line if required.

my idea, implement function this:

    func addtoattributedstring(attstring : nsmutableattributedstring, plainstring : string, font : uifont, preferredsize : cgfloat, maxwidth : cgfloat) 

and make attributed string calling text 1,2,3... , insert newline (\n) between them.

any ideas?

sizetofit() adjust height text.

    var str : nsmutableattributedstring = nsmutableattributedstring(string: "bla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla\nbla")     str.addattribute(nsfontattributename, value: uifont.systemfontofsize(20), range: nsrange(location: 13,length: 3))     button.setattributedtitle(str, forstate: uicontrolstate.normal)     button.titlelabel!.linebreakmode = .bywordwrapping     button.titlelabel?.textalignment = .center     button.titlelabel?.adjustsfontsizetofitwidth = true     button.sizetofit()     button.layoutifneeded() 

working code :

    var attrstring = nsmutableattributedstring()     addnewlinetoattributedstring(attrstring, plainstring: "big title", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "big title", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "big title", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     addnewlinetoattributedstring(attrstring, plainstring: "smaller smaller smaller text", font: uifont.systemfontofsize(10), preferredsize: 50, maxwidth: 100)     button.setattributedtitle(attrstring, forstate: uicontrolstate.normal)     button.titlelabel!.linebreakmode = .bywordwrapping     button.titlelabel?.textalignment = .center     button.titlelabel?.adjustsfontsizetofitwidth = true     button.sizetofit()     button.layoutifneeded() 

enter image description here


Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -