locked
RichTextBlock Wrap Problem

    Question

  • Hello,

    Could someone please tell me how to get the following code to wrap properly on the first line? As you can see from the image below it does the wrapping on the second line. I want the wrapping to occur on all lines.

    <RichTextBlock FontSize="40" TextWrapping="Wrap">
        <RichTextBlock.Blocks>
            <Paragraph >
                <Paragraph.Inlines>
                    <Run Text="Word thisisalongwordwraphereandIdontwantthis" />
                </Paragraph.Inlines>
            </Paragraph>
        </RichTextBlock.Blocks>
    </RichTextBlock>

    Here's how it looks like:

    Word
    thisisalongwordwrapherean
    dIdontwantthis

    Thanks

    Thursday, August 7, 2014 2:02 PM

Answers

  • You can't change the breaking algorithm. You can only change the data passed to it.

    If you add soft breaks everywhere in the word that is reasonable to break then the algorithm can break it earlier. Otherwise it will see that the word doesn't fit on the current line and drop it to the next one. It will then give up and wrap anyway when it sees that the word still won't fit.

    You could add breaks between every letter to let it break anywhere, but you'll get better results if you add them at natural pauses like between sub-words in a compound word or between syllables.

    --Rob

    • Marked as answer by Dev Help Friday, August 8, 2014 11:13 PM
    Thursday, August 7, 2014 3:57 PM
    Owner

All replies

  • Add some soft-hyphens or zero-width spaces to the long word to indicate places you want it to be able to wrap.
    Thursday, August 7, 2014 2:22 PM
    Owner
  • Hello Rob,

    The problem is that the richtextblock resizes when the window is resized; I wouldn't know where it breaks.

    I want it to look something like this:

    Word thisisalongwordwrapherean
    dIdontwantthis

    • Edited by Dev Help Thursday, August 7, 2014 2:30 PM
    Thursday, August 7, 2014 2:26 PM
  • You can't change the breaking algorithm. You can only change the data passed to it.

    If you add soft breaks everywhere in the word that is reasonable to break then the algorithm can break it earlier. Otherwise it will see that the word doesn't fit on the current line and drop it to the next one. It will then give up and wrap anyway when it sees that the word still won't fit.

    You could add breaks between every letter to let it break anywhere, but you'll get better results if you add them at natural pauses like between sub-words in a compound word or between syllables.

    --Rob

    • Marked as answer by Dev Help Friday, August 8, 2014 11:13 PM
    Thursday, August 7, 2014 3:57 PM
    Owner