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

Determining the font size of selected span(s)

1 Answer 134 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Will
Top achievements
Rank 1
Will asked on 21 Aug 2012, 06:54 PM
I'm trying to complete a toolbar-based font size editor for the RRTB.  

I'm trying to emulate the behavior of the ribbon-based editor's font size combo.  It displays the font size of the size of the span where the cursor is located.  I can do this by listening to CurrentSpanStyleChanged (as recommended elsewhere in the forum),  I can then grab the CurrentEditingStyle and update the current font size.

var style = steRTB.CurrentEditingStyle;
SelectedFontSize = (double)_conv.ConvertBack(
                       style.GetPropertyValue(TSpan.FontSizeProperty),
                       typeof(double), null, null);


Its a little bit wonky, I know, but its the only way I know of now to keep my currently displayed font size in sync.

The problem arises when there is a selection.  The correct way to display the current font size when there exist multiple font sizes within a selection is to show no font size.  The ribbon UI does this.  I'm trying to emulate this, however I can't figure out how to tell when 

1. A selection event has triggered CurrentSpanStyleChanged
2. How to determine the font sizes for each span within the selection

How can I do this?  Or how should I be keeping track of the current font size, if I'm totally off?

1 Answer, 1 is accepted

Sort by
0
Accepted
Nikolay Demirev
Telerik team
answered on 24 Aug 2012, 04:38 PM
Hello Will,

You could use this code for getting the font size of the current span or selection in the editor:

void editor_CurrentSpanStyleChanged(object sender, EventArgs e)
{
    double? commonFontSize = this.GetCommonFontSize();
    fontSizeTextBox.Text = commonFontSize == null ? "" : commonFontSize + " dip";
}
 
private double? GetCommonFontSize()
{
    double lastFontSize = this.editor.CurrentEditingStyle.SpanProperties.FontSize;
 
    var boxes = this.editor.Document.Selection.GetSelectedBoxes();
    foreach (var box in boxes)
    {
        Span span = box.AssociatedInline as Span;
        if (span != null && span.FontSize != lastFontSize)
        {
            return null; //there are at least two spans with different font size
        }
    }
    return lastFontSize;
}

I hope this helps.

Kind regards,
Nikolay Demirev
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Will
Top achievements
Rank 1
Answers by
Nikolay Demirev
Telerik team
Share this question
or