我需要以一种完全适合预定义框的方式在WPF Canvas上显示单词.
一个框通常包含一行文本,从一个字母到几个单词.
盒子内的文字必须尽可能大,即:触摸盒子的所有边框(除非由于异常的盒子/高度比而导致文本失真太多).
我找不到根据文本内容计算合适的字体高度,缩放和偏移的好方法.
原始文本宽高比不能改变的第一个解决方案已经非常好了!
我想使用TextBlock元素,但其他任何有用的东西都应该没问题.
正如Robery Levy所说,你可以用a Viewbox来实现这一点.然而,文本本身不会延伸,因此根据您的文本(正如您所注意到的那样),您仍然会在零或多个边上留下一些"边距".要解决此问题,您可以创建一个自定义控件,Geometry从a 中构建a FormattedText,然后DrawGeometry在OnRender中绘制它.你会注意到文本的质量如何随着更大的提高而提高FontSize.一个非常小的文本(例如FontSize="10")在大的文本中看起来不会很尖锐,Viewbox所以你需要进行一些实验

一些示例Xaml
<Canvas Background="Black">
<Viewbox Canvas.Left="10" Canvas.Top="10"
Stretch="Fill" Width="200" Height="50">
<Border Background="Red">
<local:StretchText Text="Text" Foreground="Green" FontSize="100"/>
</Border>
</Viewbox>
<Viewbox Canvas.Left="230" Canvas.Top="10"
Stretch="Fill" Width="200" Height="50">
<Border Background="Red">
<local:StretchText Text="B" Foreground="Green" FontSize="500"/>
</Border>
</Viewbox>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
StretchText.cs
public class StretchText : Control
{
protected override void OnRender(DrawingContext drawingContext)
{
FormattedText formattedText = new FormattedText(
Text,
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretches.Normal),
FontSize,
Foreground);
Geometry textGeometry = formattedText.BuildGeometry(new Point(0, 0));
this.MinWidth = textGeometry.Bounds.Width;
this.MinHeight = textGeometry.Bounds.Height;
TranslateTransform translateTransform = new TranslateTransform();
translateTransform.X = -textGeometry.Bounds.Left;
translateTransform.Y = -textGeometry.Bounds.Top;
drawingContext.PushTransform(translateTransform);
drawingContext.DrawGeometry(Foreground, new Pen(Foreground, 1.0), textGeometry);
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",
typeof(string),
typeof(StretchText),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4351 次 |
| 最近记录: |