سؤال

I want to decrease the font size of the paragraph to fit the RichTextBox width to prevent text wrapping when user types some text. How can I do this?

هل كانت مفيدة؟

المحلول

One way is to get the start line position of the start and the end of the paragraph and compare them:

TextPointer contentStart = _richTextBox.Document.ContentStart;
TextPointer contentEnd = _richTextBox.Document.ContentEnd;

while (true)
{
    var tpStart = contentStart.GetLineStartPosition(0);
    var tpEnd = contentEnd.GetLineStartPosition(0);
    var offset = tpStart.GetOffsetToPosition(tpEnd);
    if (offset != 0)
    {
        _richTextBox.FontSize -= 1; // at least two lines
    }
    else
    {
        break; // one line
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top