开发者

How to apply styles toa part of a string for a long string using C#

I have a long string say for example "Its a great thing to work with C#".

In the above string for a substring"Its a great" i have a seperate font and color format and for the remaining i have different font and color format. How to apply those styles and draw in an image usin开发者_运维技巧g graphics.

Expected o/p: Its a great thing to work with C#

can any one help me on this.

please, let me know if you need any more details.


Strings by themselves don't store font styles - they simply store a sequence of text characters.

If you wish to store styles, then you will need a higher level representation. The possibilities include using an existing format like RTF (Rich Text Format) or HTML or making up your own format that uses special character codes embedded in the text to indicate changes of style.

The advantage of a format like RTF or HTML is that there are WinForms controls that you can put your text into which will display the styles. An RTF control will also allow the user to edit the text. You can also copy and paste the text to/from other applications (into your code during development, and from within your running application).

If you wish to draw the text yourself as graphics, then you may prefer to invent your own format to keep your code simpler (HTML and RTF are complex formats) - or better yet - use a fixed subset of HTML so you remain standards-compatible without having to implement too much HTML parsing/rendering code of your own.


You will need to create a separate font and brush for each style of text, break up your string to the sections for each text and draw each separately.

The DrawString overloads take font and brush parameters, and you can use one of the MeasureString overloads to figure out where to place the next bit of text.

If this is a repeating requirement, you may want to wrap your logic for this in a class, for reuse.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜