Is there any easy way to add a tab or spaces to the next line created after an XRLabel word wraps down?
Here is what I am getting now.
Here is what I'm wanting to do
Is there any easy way to add a tab or spaces to the next line created after an XRLabel word wraps down?
Here is what I am getting now.
Here is what I'm wanting to do
Hello Derek,
Thank you for the update. I believe the easiest way would be to display the text using XRRichText and shift the text using RTF paragraph settings.
An advanced option is to process label text in code and wrap the text manually in the XRLabel.BeforePrint event handler. For example, as demonstrated at Word wrap a string in multiple lines.
The idea is to split the original text into words, then compile a string by adding words one by one; use the BestSizeEstimator to calculate the width of the compiled string; if the string width exceeds the original label width, append a new string. Finally, disable XRLabel.WordWrap, enable the Multiline option, and assign an array of strings to a label's Text
.
Consider this example:
C# private void xrLabel1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
var lbl = sender as XRLabel;
var res = WrapText(lbl.Text, (int)lbl.WidthF);
lbl.WordWrap = false;
lbl.Multiline = true;
lbl.Text = res;
}
string WrapText(string text, double totalTextWidth) {
string[] originalLines = text.Split(new string[] { " " }, StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
double actualWidth = 0;
foreach (var item in originalLines) {
int w = (int)BestSizeEstimator.GetBoundsToFitText(xrLabel1, item + " ").Width;
actualWidth += w;
if (actualWidth > totalTextWidth) {
wrappedLines.Add(actualLine.ToString());
actualLine.Clear();
actualWidth = w;
}
actualLine.Append(item + " ");
}
if (actualLine.Length > 0)
wrappedLines.Add(actualLine.ToString());
return string.Join(Environment.NewLine, wrappedLines.ToArray());
}
If you wish to append an indent starting with the second line, modify the WrapTest
method code:
C# string WrapText(string text, double totalTextWidth) {
string[] originalLines = text.Split(new string[] { " " }, StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
double actualWidth = 0;
string indent = " ";
foreach (var item in originalLines) {
int w = (int)BestSizeEstimator.GetBoundsToFitText(xrLabel1, item + " ").Width;
actualWidth += w;
if (actualWidth > totalTextWidth) {
var newline = wrappedLines.Count == 0 ? actualLine.ToString() : indent + actualLine.ToString();
wrappedLines.Add(newline);
actualLine.Clear();
actualWidth = w;
}
actualLine.Append(item + " ");
}
if (actualLine.Length > 0)
wrappedLines.Add(indent + actualLine.ToString());
return string.Join(Environment.NewLine, wrappedLines.ToArray());
}
Please let me know if you have any questions.
I think with some modification I will be able to get my desired output using the BeforePrint event.
Thank you very much Jannet, I appreciate the detailed explanation and quick response!
For anyone curious how I modified the WrapText method to work for me, see below. The problem I had with the original method was an indent was added to lines that didn't need to wrap.
Before:
After:
C#if (wrappedLines.Count == 0)
return string.Join(Environment.NewLine, actualLine.ToString());
if (actualLine.Length > 0)
wrappedLines.Add(indent + actualLine.ToString());
return string.Join(Environment.NewLine, wrappedLines.ToArray());
Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Hello Derek,
The desired output looks as if the text is aligned at center. Please set the label's TextAlignment to MiddleCenter and check to see if this produces the required outcome.
I look forward to your reply.
Hello Jannet,
Thanks for the information. Unfortunately I don't believe this to be my issue. What I am trying to do is force an indent at the beginning of the 2nd line every time the text wraps because the XRLabel is not wide enough to hold all of the cell content.
You can see below that there is to much text to fit into the XRLabel, and that Word Wrap is on, so the text will move down to the next line in preview.

Designer
Preview

What I want my preview to look like is the following. An indent after the word is wrapped.
