简体   繁体   English

Graphics.DrawString内联字体更改C#

[英]Graphics.DrawString inline font change C#

I'm working on creating an application that will display JSON data in a nicely formatted paragraph. 我正在创建一个应用程序,该应用程序将以格式正确的段落显示JSON数据。 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. JSON数据具有标题,副标题,正文等内容。在正文中可以有链接(我只需要将其显示为蓝色并带有下划线),这就是我遇到麻烦的地方。

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). 该程序基于.net Compact Framework,并且只给了Graphics对象和JSON对象(以及矩形,字体,颜色)。 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, ps If there is an easy way to determine the proper height of the rectangle in the DrawString method, so that no text is clipped, that would also be very helpful! 噢,ps如果在DrawString方法中有一种简单的方法可以确定矩形的正确高度,这样就不会剪切任何文本,那也将非常有帮助!

UPDATE: I figured out my PS question :) http://www.mobilepractices.com/2007/12/multi-line-graphicsmeasurestring.html 更新:我想出了我的PS问题:) 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. 好的,我最终要做的是添加一个带有特殊“ Word”对象的自定义绘制方法。 The Word object holds a string, color, and pointer to a font. Word对象包含字符串,颜色和字体指针。 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 我将文本解析为Word对象的字典,然后遍历所有对象,并使用以下sudo代码绘制文本

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. DrawWordUntilWidthDrawWordUntilWidth单词中的word (由空格和“-” curX > maxWidth ),直到curX > maxWidth ,然后绘制适合的单词,将单词中的字符串截断,将curY += maxWordHeight设置为0,将curY += maxWordHeight设置curY += maxWordHeight的最大高度。该行上使用的字体。 If all the words in word are used up, null is returned, else the trunc'ed word is returned. 如果单词中的所有word都用完了,则返回null,否则返回被删节的单词。

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. DrawWord非常相似,但是假设curX始终为0,因此它可以绘制整个字符串,然后相应地设置curX和curY。

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! 如果有人有更好的解决方案,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM