Today I was think how to optimize the drawing string method on my library... so far I worked out this... However I still couldn't workout how to really wrap long string within the sentence...
Any help?
publicstaticstring[]WrapString(thisGraphicsgx,Fontfont,stringtext,floatmaxWidth,boolwrap){// find if actual max text width is smaller than max width or maxWidth is smaller than zero or wrap is set to falseif(gx.MeasureString(text,font).Width<maxWidth||maxWidth<=0||!wrap){returntext.Split(newchar[]{'\n'});}intmaxChars=(int)(maxWidth/(gx.MeasureString("ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789",font).Width/37));//text = text.BreakLongString(maxChars);text=text.Replace(Environment.NewLine,"\n");text=text.Replace("\r","");text=text.Replace("\t"," ");string[]words=text.Split(newchar[]{' '});List<string>lines=newList<string>((int)(text.Length/maxChars));stringcurrentLine="";for(inti=0;i<words.Length;i++){// if the word is empty, then repace it to one spaceif(string.IsNullOrEmpty(words[i]))words[i]=" ";floatcurrWidth=gx.MeasureString(currentLine+" "+words[i],font).Width;// check if the current width is greater than max lengthif(currWidth<maxWidth){// if first entry, then put current line to the first wordif((lines.Count==0)&&string.IsNullOrEmpty(currentLine))currentLine=words[i];else{// if not, append each word to current linecurrentLine+=" "+words[i];// check if the currentline has \n in there.string[]newLines=currentLine.Split('\n');// if it does, then add a new lineif(newLines.Length>1){// do not loop to last as it will be the new currentlinefor(intj=0;j<newLines.Length-1;j++)lines.Add(newLines[j]);// the current line is the last line of the new linescurrentLine=newLines[newLines.Length-1];}}}else{// if the currentline width is greater than max width// then add a new line to the listif(!string.IsNullOrEmpty(currentLine))lines.Add(currentLine);// make the current line to the last wordcurrentLine=words[i];}}// if still has word, add it to the listif(!string.IsNullOrEmpty(currentLine))lines.Add(currentLine);returnlines.ToArray();}
The standard paragraphs Welcome to this demo page! Here, you’ll get an exclusive preview of our cutting-edge platform designed to revolutionise your digital experience. Our...