Быстрый метод - измерить строку (words
), которую нужно прокрутить, с помощью TextRenderer.MeasureText , разделить меру width
на части, равную количеству символов в строке, и использовать ScrollToHorizontOffset () для выполнения прокрутки:
public async void textRotation()
{
float textPart = TextRenderer.MeasureText(words, new Font(Text.FontFamily.Source, (float)Text.FontSize)).Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(100);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
То же самое, но с использованием класса FormattedText для измерения строки:
public async void textRotation()
{
var textFormat = new FormattedText(
words, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight,
new Typeface(this.Text.FontFamily, this.Text.FontStyle, this.Text.FontWeight, this.Text.FontStretch),
this.Text.FontSize, null, null, 1);
float textPart = (float)textFormat.Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(200);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
