android - How to prevent URLs from being wrapped in TextView? -
i have multiline textview
can display optional url. have problem: of long urls displayed wrapped in position of ://
sometext sometext http:// <-- auto line wrap google.com/
how can disable wrapping whole url or @ to the lowest degree http(s)://
prefix? still need text wrapping enabled however.
my text should wrap that
sometext sometext <-- auto line wrap http://google.com/
this proof of concept implement custom wrap textview. may need add/edit conditions according requirement.
if requirement our textview class must show multiline in such way should not end text ever (here http:// , http:), have modified code of popular textview class on meet requirement: source : auto scale textview text fit within bounds
changes:
private boolean mcustomlinewrap = true; /** * resize text size specified width , height * @param width * @param height */ public void resizetext(int width, int height) { charsequence text = gettext(); // not resize if view not have dimensions or there no text if(text == null || text.length() == 0 || height <= 0 || width <= 0 || mtextsize == 0) { return; } // text view's paint object textpaint textpaint = getpaint(); // store current text size float oldtextsize = textpaint.gettextsize(); // if there max text size set, utilize lesser of , default text size float targettextsize = mmaxtextsize > 0 ? math.min(mtextsize, mmaxtextsize) : mtextsize; // required text height int textheight = gettextheight(text, textpaint, width, targettextsize); // until either fit within our text view or had reached our min text size, incrementally seek smaller sizes while(textheight > height && targettextsize > mmintextsize) { targettextsize = math.max(targettextsize - 2, mmintextsize); textheight = gettextheight(text, textpaint, width, targettextsize); } if(mcustomlinewrap ) { // draw using static layout staticlayout layout = new staticlayout(text, textpaint, width, alignment.align_normal, mspacingmult, mspacingadd, false); // check have to the lowest degree 1 line of rendered text if(layout.getlinecount() > 0) { string linetext[] = new string[layout.getlinecount()]; // since line @ specific vertical position cutting off, // must trim previous line string wrapstr = "http:", wrapstrwithslash = "http://"; boolean preappendwrapstr = false, preappendwrapstrwithslash = false ; for(int lastline = 0; lastline < layout.getlinecount(); lastline++) { int start = layout.getlinestart(lastline); int end = layout.getlineend(lastline); linetext[lastline] = ((string) gettext()).substring(start,end); if(preappendwrapstr) { linetext[lastline] = "\n" + wrapstr + linetext[lastline]; preappendwrapstr = false; } else if(preappendwrapstrwithslash) { linetext[lastline] = "\n" + wrapstrwithslash + linetext[lastline]; preappendwrapstrwithslash = false; } if(linetext[lastline].endswith(wrapstr)) { preappendwrapstr = true; linetext[lastline] = linetext[lastline].substring(0,linetext[lastline].lastindexof(wrapstr)); } if( linetext[lastline].endswith(wrapstrwithslash)) { preappendwrapstrwithslash = true; linetext[lastline] = linetext[lastline].substring(0,linetext[lastline].lastindexof(wrapstrwithslash)); } } string compstring = ""; for(string linestr : linetext) { compstring += linestr; } settext(compstring); } } // devices seek auto adjust line spacing, forcefulness default line spacing // , invalidate layout side effect textpaint.settextsize(targettextsize); setlinespacing(mspacingadd, mspacingmult); // notify listener if registered if(mtextresizelistener != null) { mtextresizelistener.ontextresize(this, oldtextsize, targettextsize); } // reset forcefulness resize flag mneedsresize = false; }
android textview textwrapping
No comments:
Post a Comment