ios - Multiline UIButton autosize per line -
i want use standard uibutton
text want put on uibutton
has...
- ... multiple lines (each line different string) , ...
- ... each line should have different font , size ...
- ... automatically resizes font fit button width (not height make bit easier)
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()
Comments
Post a Comment