system.drawing - How to draw a multi-line string with trailing spaces in .NET -


i drawing strings using graphics.drawstring , want trailing spaces included use stringformatflags.measuretrailingspaces. single line strings works fine noticed new line characters result in white space @ end of line. not want (and wonder would).

so want implement mydrawstring method creates following output. | the x coordinate of right-aligned draw operation. implementation should work centered strings.

mydrawstring("test")              ->       test|  mydrawstring("test   ")           ->    test   |  mydrawstring("test\r\nline 2   ") ->       test|                                       line 2   | 

unfortunately, output of last call looks more like

                                         test  |                                       line 2   | 

is there way achieve want without breaking string , drawing line line?

if not, best approach draw lines individually @ same vertical positions single drawstring call use? measurestring().height did not give me full distance between 2 lines.

update

i found this msdn page explains how determine line height when drawstring draws multi-line string. still i'd prefer use single drawstring call takes care of vertical alignment of individual lines entire text block. hints?

i figured out in case have bounding rectangle of text makes custom implementation quite easy.

    public shared sub mydrawstring(byval g drawing.graphics, _                                    byval text string, _                                    byval font drawing.font, _                                    byval brush drawing.brush, _                                    byval layoutrect drawing.rectanglef, _                                    byval stringformat drawing.stringformat)          dim linespacingpixel single         linespacingpixel = font.size * font.fontfamily.getlinespacing(font.style) / _             font.fontfamily.getemheight(font.style)          dim lines() string         lines = text.split(new string() {vbcrlf}, stringsplitoptions.none)          = 0 lines.count - 1             dim rect drawing.rectanglef             rect = new drawing.rectanglef(layoutrect.x, layoutrect.y + * linespacingpixel, _                                           layoutrect.width, _                                           layoutrect.bottom - layoutrect.y - * linespacingpixel)              g.drawstring(lines(i), font, brush, rect, stringformat)         next      end sub 

note code adjustments required take word wrap , linealignment account if size of layoutrect did not match size of text drawn.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -