android - How to get height of text with fixed width and get text length which fits in a frame? -
well, i've managed fit questions in title. need break long text in columns/frames , layout them in view. i've been digging solutions couple of days now, can't find examples or clear documentation on how complete of tasks. i've seen mentions of staticlayout, don't know how use properly. text height i've tried textpaint's gettextbounds method, doesn't have width limit , looks measures single line (well, maybe doing wrong).
maybe has example of staticlayout or it's subclass usage?
everything looks simple "on paper": create "frame", check how characters fits in it, fill frame , position it, repeat until end of text , yet can't find on how :)
i don't know exact answer question, can calculate width , height of text based on font type , size using methods available in graphical library.
i've done in c# , java. in c# it's called "measurestring", , in java, "fontmetrics".
edit:
see if code useful ( haven't compiled because don't have android sdk here):
string mytext=""; string tempstr=""; int startindex=0; int endindex=0; //calculate end index fits endindex=mypaint.breaktext(mytest, true, framewidth, null)-1; //substring fits frame tempstr=mytext.substring(startindex,endindex); while(endindex < mytext.length()-1) { //draw or add tempstr frame //at point //set new start index startindex=endindex+1; //substring remaining of text tempstr=mytext.substring(startindex,mytext.length()-1); //calculate end of index fits endindex=mypaint.breaktext(tempstr, true, framewidth, null)-1; //substring fits frame tempstr=mytext.substring(startindex,endindex); }
similar methods available android:
breaktext(string text, boolean measureforwards, float maxwidth, float[] measuredwidth)
measure text, stopping if measured width exceeds maxwidth.
measuretext(string text)
return width of text.
http://developer.android.com/reference/android/graphics/paint.html
Comments
Post a Comment