Siana

Hello all,

i want to calculate if the text size actually overflows teh visible region and appropriately format it with some ellipsis. I need to use a textbox only and not a text block.

I did try using the function to calculate the size of the GetRectFromCharacterIndex, But I am not getting the formulas correctly ...

looks like I am stoned after a long day ...

has any one done this before

Sophia



Re: Windows Presentation Foundation (WPF) Calculating the actual text size on the screen

Yi-Lun Luo - MSFT

Hello, try this:

ScrollViewer sv = (ScrollViewer)text1.Template.FindName("PART_ContentHost", text1);

double textLength = text1.FontSize * text1.Text.Length;

if (textLength > sv.ActualWidth)

{

//Show ellipsis.

}






Re: Windows Presentation Foundation (WPF) Calculating the actual text size on the screen

soniadaku

This is what I did ....

int i = 0;

for (i = Text.Length; i >= 0; i--)

{

string someString = Text.Substring(0, i) + "...";

FormattedText formattedText = new FormattedText(

someString, System.Globalization.CultureInfo.CurrentCulture,

FlowDirection,

new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch),

this.FontSize,

System.Windows.Media.Brushes.Black);

if (formattedText.Width <= Width)

{

break;

}

}

if (i < Text.Length && i >= 3)

{

Text = Text.Substring(0, i - 2);

Text = Text + "...";

}





Re: Windows Presentation Foundation (WPF) Calculating the actual text size on the screen

soniadaku

Thanks for this approach as well... mcuh more easier than the creating formatted text.