简体   繁体   English

如何使文本的字体大小自动适合其容器?

[英]How to make the text's fontsize auto fit its container?

In my Wpf prroject, a TextBlock is used to show app title.在我的 Wpf 项目中,TextBlock 用于显示应用程序标题。 And to support multiple languages, the text is associated with a resource.为了支持多种语言,文本与资源相关联。

While the TextBlock's parent UI container's size is fixed.而 TextBlock 的父 UI 容器的大小是固定的。 And for different language, the app title's size might differ a lot.对于不同的语言,应用标题的大小可能会有很大差异。

In below code the font size of the Text is fixed to 20, that will cause UI problem when in some language.在下面的代码中,Text 的字体大小固定为 20,这在使用某些语言时会导致 UI 问题。 How to make the font size auto fit?如何使字体大小自动适应? Any idea?任何想法?

<Border Grid.Row="0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="{DynamicResource OCR.OCRWindow.TitleBorder}">
    <Grid Name="OCRTitleBar" Background="{DynamicResource OCR.OCRWindow.TitleBar}">
        <TextBlock Text="{x:Static prop:Resources.AppTitle}" FontSize="20" Foreground="White"
               HorizontalAlignment="Left" VerticalAlignment="Center" Margin="24,0,0,0"/>

Maybe wrapping the TextBlock inside a ViewBox scales the FontSize correctly也许将 TextBlock 包装在 ViewBox 中可以正确缩放 FontSize

<Viewbox Stretch="Uniform">
       <TextBlock Text="{x:Static prop:Resources.AppTitle}" FontSize="20" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="24,0,0,0"/>
</Viewbox>

There is a msdn article about setting up different stretch properties How to: Apply Stretch Properties to the Contents of a Viewbox有一篇关于设置不同拉伸属性的 msdn 文章How to: Apply Stretch Properties to the Contents of a Viewbox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何相对于其容器调整FlowDocuments字体大小? - How to adjust a FlowDocuments fontsize relative to its container? 如何在iTextsharp中将文本(将Fontsize更改为适合大小)调整为Rectangle - How to fit text (change Fontsize until it fits) into Rectangle in iTextsharp 如何使StackPanel适应其孩子的宽度 - How to make a StackPanel fit its Children's width 如何在检查器中创建文本/字符串容器以添加描述/信息文本以及如何适应并将其转换为 ui 文本组件? - How can I make a text/string container in the inspector for adding descriptions/info text and how to fit and convert it to ui text component? 如何使用 CustomWebViewRenderer 使 WKWebView 的高度适合其在 Xamarin 中的内容? - How do I make WKWebView's height fit its contents in Xamarin with a CustomWebViewRenderer? 通过动态更改字体大小来调整按钮控件中的文本 - Fit the text in the button control on resize by changing Fontsize dynamically 当Container调整高度时,调整ListViewItems的FontSize - Adjust FontSize for ListViewItems when Container resizes its height 更改FontSize以适合TextBlock - Change FontSize to fit TextBlock 如何使LVI延伸到其容器的宽度? - How to make LVI stretch to width of its container? 如何使 NavigationView 的内容适合屏幕 - How to make NavigationView's content fit to screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM