Nativescript: dynamic font size for multiline label to fit into container on Android -
i need set font size multiline label dynamically depending on container dimensions. whole text should fit container evenly. if text short font should bigger, if long - smaller. set maximum , minimum allowed font size , maximum allowed lines number. means if text long , can not fit container given minimum font size , maximum lines number should truncated ellipsis.
i managed on ios, not achieve same on android.
here ios code:
function settextforlabelios( label: any, maxfontsize: number, minfontsize: number, maxlines: number ) { let numlines: number = 1 const font = label.font // uifont const textsize = nsstring.stringwithstring( label.text ).sizewithattributes( <any>{ [ nsfontattributename ]: font } ) const textwidth = math.ceil( nsutils.layout.todevicepixels( textsize.width ) ) const textheight = math.ceil( nsutils.layout.todevicepixels( textsize.height ) ) while ( ( ( textwidth / numlines ) / ( textheight * numlines ) > labelcontainerwidth / labelcontainerheight ) && numlines < maxlines ) { numlines += 1 } label.adjustsfontsizetofitwidth = true label.numberoflines = numlines label.minimumscalefactor = minfontsize / maxfontsize }
(ios solution based on https://stackoverflow.com/a/44103978/3595434)
Comments
Post a Comment