public string GetHiddenRangeOfTrimmedText(TextBlock textBlock)
{
var typeFace = new Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch);
var numberSubstitution = new NumberSubstitution(NumberSubstitution.GetCultureSource(textBlock), CultureInfo.CurrentUICulture, NumberSubstitution.GetSubstitution(textBlock));
// Define ellipsis using four ellipsis characters (instead of three).
// This is because ellipsis is rendered using three visible and one hidden ellipsis characters.
var formattedEllipsisText = new FormattedText(
"....",
CultureInfo.CurrentUICulture,
textBlock.FlowDirection,
typeFace,
textBlock.FontSize,
textBlock.Foreground,
numberSubstitution,
VisualTreeHelper.GetDpi(textBlock).PixelsPerDip);
double clippingOffset = textBlock.ActualWidth - formattedEllipsisText.Width - textBlock.Padding.Right;
var clippingPosition = new Point(clippingOffset, 0);
TextPointer clippingPositionTextPointer = null;
do
{
try
{
clippingPositionTextPointer = textBlock.GetPositionFromPoint(clippingPosition, true);
}
catch (ArgumentException)
{
if (clippingPosition.X == 0)
{
return string.Empty;
}
clippingPosition.Offset(-1, 0);
}
if (clippingPositionTextPointer is not null)
{
break;
}
} while (clippingPosition.X >= 0);
var clippedTextRange = new TextRange(clippingPositionTextPointer, textBlock.ContentEnd);
string clippedText = clippedTextRange.Text;
return clippedText;
}
public int GetIndexOfHiddenRangeOfTrimmedText(TextBlock textBlock)
{
string hiddenTextRange = GetHiddenRangeOfTrimmedText(textBlock);
int indexOfHiddenTextRange = textBlock.Text.IndexOf(hiddenTextRange, StringComparison.OrdinalIgnoreCase);
return indexOfHidenTextRange;
}
1条答案
按热度按时间insrf1ej1#
您需要手动测量文本(应用所有字体样式)。
如果
TextTrimming
设置为TextTrimming.WordEllipsis
,一般的解决方案必须迭代地找到最后一个字符的位置。因为没有相关的API,我们必须用艰难的方式来做,反复测试,直到我们不再得到异常为止。没有其他方法(要求知道剪辑可用文本区域的确切文本部分通常是不明智的)。
但是,当
TextTrimming
设置为TextTrimming.CharacterEllipsis
时,不会抛出任何异常。这是因为基于字符的修剪产生了一个可预测的结果,而不是词的长度是随机的(因为词是未知的)字修剪。这意味着基于TextTrimming.CharacterEllipsis
的情况更友好。此外,查找多行
TextBlock
的多个隐藏文本范围是一个更复杂的场景,所提出的解决方案没有解决。支持
TextTrimming.WordEllipsis
和TextTrimming.CharacterEllipsis
的最终解决方案如下所示:字符串