I'm working on creating an application that will display JSON data in a nicely formatted paragraph. The JSON data has things like a title, sub-title, body, etc. In the body there can be links (which I just need to display as blue and underlined) And this is where I'm running into trouble.
The program is based on the .net Compact Framework and I'm only given a Graphics object along with the JSON object (and a rectangle, font, color). Is there anyway to nicely draw a string of text with all of the links changed to the different color/font?
My thoughts have been to draw each individual word and just change the font right then when the word comes up. This sounds very inefficient. Is there a better way?
Thanks :)
Oh, p.s. If there is an easy way to determine the pro开发者_如何学JAVAper height of the rectangle in the DrawString method, so that no text is clipped, that would also be very helpful!
UPDATE: I figured out my P.S. question :) http://www.mobilepractices.com/2007/12/multi-line-graphicsmeasurestring.html
Ok, what I ended up doing is adding a custom drawing method w/ special "Word" objects. The Word object holds a string, color, and pointer to a font. I parsed the text into a dictionary of Word objects and then looped through all the objects and drew the text with the following sudo code
int maxWidth = Width
int curX = 0
int curY = 0
foreach word in words
if curX > 0
word = DrawWordUntilWidth(out curX, out curY, word, maxWidth)
if word != null
DrawWord(out curX, out curY, word, maxWidth)
DrawWordUntilWidth
will mesure words in word
(deliminated by spaces and "-") until curX > maxWidth
and then draw the words that will fit, trunc the string in word, set curX to 0, set curY += maxWordHeight
being the max height of the fonts used on that row. If all the words in word
are used up, null is returned, else the trunc'ed word is returned.
DrawWord
is very similar but runs on the assumption that curX is always = 0, so it can draw the whole string then set the curX and curY accordingly.
It works pretty well, as in there are few studders and it renders pretty smoothly. If anyone has a better solution, please let me know!
精彩评论