I have a label that displays on more than a line and I would like to justify the text in it (align left and right). What is the best way to achieve that?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Unfortunately only the three most basic and simple types of alignment are supported:
Right
,Left
andCenter
.The fourth one,
Justified
orBlock
, is not supported in any .NET control afaik, not even in aRichtTextBox
:-(The only workaround would be to add either spaces or better a smaller whitespace character like
thin space
(U+2009) orhair space
(U+200A) between the words i.e. after the regular spaces until theLabel
'sHeight
changes. Then step one back and try to find the next insertion point, i.e. the next line and so on.. until the end of the text is reached.A little tricky but not terribly hard.
Another implementation.
This one inserts "Hair Spaces" between words only.
EDIT:
Added a method that implements paragraph Block Align.
Both
JustifyParagraph()
andJustifyLine()
call the worker methodJustify()
.EDIT2:
Method call changed.
JustifyLines()
only deals with single lines of text (shorter than the client area)The worker method
About the RichTextBox.
@TaW says that it doesn't support Block Align, but this is not exactly true.
RichTextBox is notoriously based on the RichEdit class and that class support "Justification".
This is reported in the old Platform SDK (with examples).
RichTextBox has its AdvancedTypographicsOption explicitly truncated during handle creation.
(It's not about implementing PARAFORMAT vs. PARAFORMAT2 structs, that's irrelevant, it's deliberate).
So this is a "cure" for poor RichTextBox. A class that derives from it and overrides OnHandleCreated to re-enable "Justification".
Works on a paragraph level or from a point onward.
This is an implementation of the solution proposed by TaW. It is just the base code for the implementation - no automatic re-adjustments, etc.
In action: