Determining the font size of selected span(s)

2 posts, 1 answers
  1. Will
    Will avatar
    11 posts
    Member since:
    Apr 2012

    Posted 21 Aug 2012 Link to this post

    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(
                           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?
  2. Answer
    Nikolay Demirev
    Nikolay Demirev avatar
    136 posts

    Posted 24 Aug 2012 Link to this post

    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.

Back to Top