This is a migrated thread and some comments may be shown as answers.

Insert LineBreak with padding to a given position (code sample)

6 Answers 142 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 21 May 2012, 11:57 PM
I've written some code that insert a line break and pads the new line to a specified position along the document X axis.

I needed this code as you cannot insert new lines with indent properties. Only paragraphs can have indent properties.

So why not use a paragraph you say? Well, I needed to be able to wrap custom annotations around multiple lines which cannot be done with Paragraphs.

Main Method: When inserting into the document you will need to change the Style that I have used (see inside the while loop). My style just highlights the blank padding so I can visually see what's going on.
public static void InsertLineBreakWithSpacing(this RadDocument document, PointF positionSpacing)
{
        //Insert line break at caret position
        document.InsertLineBreak();
 
        //Insert line spaces for padding until position is found
        while(positionSpacing.X > document.CaretPosition.Location.X)
        {
            document.Insert(" ", RadDocumentStyles.LineBreakSpacing());
        }
}


Method Call:
//Pass - reference to RadDocument - Location to pad line out to
RadDocumentExtensions.InsertLineBreakWithSpacing(radRichTextBox.Document,radRichTextBox.Document.CaretPosition.Location);

Here's my Span Style to highlight the new line padding as I previously mentioned:
public static StyleDefinition LineBreakSpacing()
{
    StyleDefinition lineBreak = new StyleDefinition();
    lineBreak.Type = StyleType.Character;
    lineBreak.SpanProperties.HighlightColor = Colors.LightGreen;
    lineBreak.DisplayName = "lineBreakSpacing";
    lineBreak.Name = "lineBreakSpacing";
 
    return lineBreak;
}

I hope this helps you.

Please let me know if you know of a better way to achieve this.

Thanks,

Rob


6 Answers, 1 is accepted

Sort by
0
Accepted
Alex
Telerik team
answered on 24 May 2012, 09:51 AM
Hello,

The approach you've taken seems to be correct. The only improvement we could suggest is to use one instance of the LineBreakSpacing style instead of creating a new instance for every inserted space. Ultimately, you can add all the styles you need when initializing the document and then get them form the document's StyleRepository by name when you need them. 

Kind regards,
Alex
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 10:09 AM
Thanks for your suggestion, I will give it a try.
I do find there is a little performance lag when inserting a lot of character spaces towards the end of a given line.

I will post my update when I have done it.

Thanks,

Rob
0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 12:08 PM
Hi Alex,

I'm having trouble applying the span style from the repository. I can get it to work with Paragraphs but not spans.

Here's my code.

Add styles to repository:
public static void AddStylesToDocumentRepository(RadDocument document)
{
document.StyleRepository.Add(ParagraphEditableGroup());
document.StyleRepository.Add(ParagraphRoomReadOnly());
document.StyleRepository.Add(LineBreakSpacing());
}


LineBreak Style Definition:
public static StyleDefinition LineBreakSpacing()
{
StyleDefinition lineBreak = new StyleDefinition();
lineBreak.Type = StyleType.Character;
lineBreak.SpanProperties.HighlightColor = Colors.PaleVioletRed;
lineBreak.DisplayName = "LineBreak Spacing";
lineBreak.Name = "lineBreakSpacing";
return lineBreak;
}



Insert LineBreak With Spacing:

public static void InsertLineBreakWithSpacingAndAnnotation(this RadRichTextBox radRichTextBox, PointF positionSpacing, int lineBreakID)
{
    //RadDocument
    RadDocument document = radRichTextBox.Document;
 
    //Insert line break at caret position
    document.InsertLineBreak();
 
    //Insert line spaces for padding until position is found
    while(positionSpacing.X > document.CaretPosition.Location.X)
    {
        Span span = new Span(" ");
        //span.Style = RadDocumentStyles.LineBreakSpacing();
        document.InsertInline(span);
    }
 
    //Setup and Get positions of text inserted (starts at end position of inserted text)
    //Start position is set once text has been inserted
     DocumentPosition startPosition = new DocumentPosition(document.CaretPosition);
    DocumentPosition endPosition = new DocumentPosition(startPosition);
 
    //Set selection range to add annotation
    //move to end of line
    startPosition.MoveToCurrentLineStart();
 
    //Set Style
    radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.StyleName = "lineBreakSpacing";
 
    //move infront of span containing line break marker
    startPosition.MoveToPreviousSpanBox();
 
    //Set selection ranges
    document.Selection.SetSelectionStart(startPosition);
    document.Selection.AddSelectionEnd(endPosition);
 
    //Set Custome Annotation
    LineBreakSpacingRangeEnd rangeEnd = new LineBreakSpacingRangeEnd();
    LineBreakSpacingRangeStart rangeStart = (LineBreakSpacingRangeStart)rangeEnd.CreatePairedStart();
    rangeStart.Name = "NewLine " + lineBreakID.ToString();
 
    //Place annotation around newly inserted text
    document.InsertCustomAnnotationRange(startPosition, endPosition, rangeStart, rangeEnd);
}


Any ideas why the style won't set to the linebreak and padding spans?
As I previously mentioned, I can apply styles to Paragraphs using the StyleRepository but not spans.

Thanks for your time,

Rob
0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 12:08 PM

This bit of code sets the style:

radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.StyleName = "lineBreakSpacing";

However, it doesn't apply it correctly to my span with the padding spaces. It is setting the style to the spans either side of the NewLineSpacing.

Here's the updated InsertNewLine method:

public static void InsertLineBreakWithSpacingAndAnnotation(this RadRichTextBox radRichTextBox, PointF positionSpacing, int lineBreakID)
{
//RadDocument
RadDocument document = radRichTextBox.Document;
//Insert line break at caret position
document.InsertLineBreak();
//Insert line spaces for padding until position is found
while(positionSpacing.X > document.CaretPosition.Location.X)
{
Span span = new Span(" ");
//span.Style = RadDocumentStyles.LineBreakSpacing();
document.InsertInline(span);
}
//Setup and Get positions of text inserted (starts at end position of inserted text)
//Start position is set once text has been inserted
DocumentPosition startPosition = new DocumentPosition(document.CaretPosition);
DocumentPosition endPosition = new DocumentPosition(startPosition);
//Set selection range to add annotation
//move to end of line
startPosition.MoveToCurrentLineStart();
//Set Style
radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.StyleName = "lineBreakSpacing";
//move infront of span containing line break marker
startPosition.MoveToPreviousSpanBox();
//Set selection ranges
document.Selection.SetSelectionStart(startPosition);
document.Selection.AddSelectionEnd(endPosition);
//Set Custome Annotation
LineBreakSpacingRangeEnd rangeEnd = new LineBreakSpacingRangeEnd();
LineBreakSpacingRangeStart rangeStart = (LineBreakSpacingRangeStart)rangeEnd.CreatePairedStart();
rangeStart.Name = "NewLine " + lineBreakID.ToString();
//Place annotation around newly inserted text
document.InsertCustomAnnotationRange(startPosition, endPosition, rangeStart, rangeEnd);
}

Thanks,

Rob

0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 12:23 PM
Ah, I  spoke too soon. I noticed where I was going wrong.

I should have done this instead:
//Set Style
//radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.StyleName = "lineBreakSpacing";
startPosition.GetCurrentSpanBox().AssociatedSpan.StyleName = "lineBreakSpacing";

However, when I continue to type after the LineBreakSpacing, the style shouldn't be applied to the typed text. It should only be applied to the LineSpacingSpan.

Here's my XAML output before line spacing:
<t:Section>
  <t:Paragraph>
    <t:Span Text="Background here should be white" />
  </t:Paragraph>
</t:Section>


Here's my XAML output after line spacing:
<t:Section>
  <t:Paragraph>
    <t:Span StyleName="lineBreakSpacing" Text="Background here should be white" />
    <custom1:LineBreakSpacingRangeStart AnnotationID="1" Name="NewLine 2" />
    <t:Span StyleName="lineBreakSpacing" Text="¬                                                       " />
    <custom1:LineBreakSpacingRangeEnd AnnotationID="1" />
    <t:Span StyleName="lineBreakSpacing" Text="Background here should be white" />
  </t:Paragraph>
</t:Section>

This is my desired XAML when Linespacing:
<t:Section>
  <t:Paragraph>
    <t:Span Text="Background here should be white" />
    <custom1:LineBreakSpacingRangeStart AnnotationID="1" Name="NewLine 2" />
    <t:Span StyleName="lineBreakSpacing" Text="¬                                                       " />
    <custom1:LineBreakSpacingRangeEnd AnnotationID="1" />
    <t:Span Text="Background here should be white" />
  </t:Paragraph>
</t:Section>

I have also attached an image of what my output look likes (this isn't how I want it to be).


Thanks,

Rob


0
Robert
Top achievements
Rank 1
answered on 24 May 2012, 05:10 PM
ah ok I've got it. I should have set the Span style after I insert my custom annotations.

Here's the final working method (please let me know if there's a better way to apply the span style):
public static void InsertLineBreakWithSpacingAndAnnotation(this RadRichTextBox radRichTextBox, PointF positionSpacing, int lineBreakID)
{
    //RadDocument
    RadDocument document = radRichTextBox.Document;
 
    //Insert line break at caret position
    document.InsertLineBreak();
 
    //Insert line spaces for padding until position is found
    while(positionSpacing.X > document.CaretPosition.Location.X)
    {
        Span span = new Span(" ");
        //span.Style = RadDocumentStyles.LineBreakSpacing();
        document.InsertInline(span);
    }
 
    //Setup and Get positions of text inserted (starts at end position of inserted text)
    //Start position is set once text has been inserted
     DocumentPosition startPosition = new DocumentPosition(document.CaretPosition);
    DocumentPosition endPosition = new DocumentPosition(startPosition);
 
    //Set selection range to add annotation
    //move to end of line
    startPosition.MoveToCurrentLineStart();
 
    //move infront of span containing line break marker
    startPosition.MoveToPreviousSpanBox();
 
    //Set selection ranges
    document.Selection.SetSelectionStart(startPosition);
    document.Selection.AddSelectionEnd(endPosition);
 
    //Set Custome Annotation
    LineBreakSpacingRangeEnd rangeEnd = new LineBreakSpacingRangeEnd();
    LineBreakSpacingRangeStart rangeStart = (LineBreakSpacingRangeStart)rangeEnd.CreatePairedStart();
    rangeStart.Name = "NewLine " + lineBreakID.ToString();
 
    //Place annotation around newly inserted text
    document.InsertCustomAnnotationRange(startPosition, endPosition, rangeStart, rangeEnd);
 
    //Set LineSpacing Span Style
    startPosition.GetCurrentSpanBox().AssociatedSpan.StyleName = "lineBreakSpacing";
}


See the attached image for a Snap show of my document.

Thanks,

Rob


Tags
RichTextBox
Asked by
Robert
Top achievements
Rank 1
Answers by
Alex
Telerik team
Robert
Top achievements
Rank 1
Share this question
or