Graphics.DrawString 内联字体更改 C#

发布于 2024-10-07 01:53:45 字数 567 浏览 0 评论 0原文

我正在创建一个应用程序,它将在格式良好的段落中显示 JSON 数据。 JSON 数据具有标题、副标题、正文等内容。正文中可以有链接(我只需将其显示为蓝色并带下划线),这就是我遇到麻烦的地方。

该程序基于 .net Compact Framework,我只得到一个 Graphics 对象和 JSON 对象(以及一个矩形、字体、颜色)。无论如何,是否可以很好地绘制一串文本,并将所有链接更改为不同的颜色/字体?

我的想法是画出每个单独的单词,然后在单词出现时立即更改字体。这听起来效率很低。有更好的办法吗?

谢谢:)

哦,ps如果有一种简单的方法可以确定DrawString方法中矩形的正确高度,这样就不会剪切任何文本,那也会非常有帮助!

更新:我想出了我的 PS 问题:) http://www.mobilepractices.com/2007/12/multi-line -graphicsmeasurestring.html

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 proper 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-10-14 01:53:45

好吧,我最终要做的是添加带有特殊“Word”对象的自定义绘图方法。 Word 对象包含字符串、颜色和指向字体的指针。我将文本解析为 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 将测量 word 中的单词(由空格分隔)和“-”)直到 curX > maxWidth,然后绘制适合的单词,截断单词中的字符串,将 curX 设置为 0,将 curY += maxWordHeight 设置为该行使用的字体的最大高度。如果word中的所有单词都用完,则返回null,否则返回被截断的单词。

DrawWord 非常相似,但运行时假设 curX 始终 = 0,因此它可以绘制整个字符串,然后相应地设置 curX 和 curY。

它的效果非常好,因为几乎没有螺柱,并且渲染得非常流畅。如果有人有更好的解决方案,请告诉我!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文