简体   繁体   English

调整标签大小

[英]Resizing Labels

I have a chart in WPF with a lot of labels. 我在WPF中有一个带有很多标签的图表。 The text on these labels is dynamically loaded and subject to change. 这些标签上的文本是动态加载的,并且随时可能更改。 If I set the width just to auto, then these labels may overlap, which makes the text unreadable. 如果我将宽度设置为自动,则这些标签可能会重叠,从而使文本不可读。

The chart support multiple sizes, so if it gets larger, then the bars are re sized and there is more space for text. 图表支持多种尺寸,因此,如果图表变大,则将调整条形图的大小,并留出更多的文本空间。 Now I want to adjust the text to the space which is available. 现在,我想将文本调整为可用的空间。 If it gets too small, I don't want to display the label anymore (a tooltip is available, so the user still gets the required information). 如果它变得太小,我不想再显示标签(可以使用工具提示,因此用户仍然可以获得所需的信息)。 Consider the string "Case 1, blah blah", there is probably not enough space to display the whole string, but just the first word. 考虑字符串“ Case 1,blah blah”,可能没有足够的空间来显示整个字符串,而只是第一个单词。 In this case I want the string to be "Case 1..", with .. indicating that there is some more information in the tooltip. 在这种情况下,我希望字符串为“ Case 1 ..”,并带有..指示工具提示中还有更多信息。

I can determine the length available for the string. 我可以确定字符串的可用长度。 But how can I determine the space a single letter will take? 但是,如何确定单个字母所占的空间? Of course I could also just re size the label, but then it would just cut off the string anywhere which is probably not helpful for the user (and looks ugly). 当然,我也可以只是调整标签的大小,但是随后它会在任何地方切断字符串,这可能对用户没有帮助(而且看起来很丑)。

Any ideas? 有任何想法吗?

If you can use TextBlocks instead of labels then they have a TextTrimming property which will do this for you to either the nearest character or the nearest word. 如果可以使用TextBlocks代替标签,则它们具有TextTrimming属性,该属性将为您做到最接近的字符或最接近的单词。


While you seem happy with the TextTrimming property, I'll edit this to add that the TextBox control has a GetRectFromCharacterIndex method that would allow you to find out the size on screen of one or more characters as long as the font settings matched your label. 尽管您似乎对TextTrimming属性感到满意,但我将对其进行编辑以添加TextBox控件具有GetRectFromCharacterIndex方法,该方法将允许您在屏幕上找到一个或多个字符的大小,只要字体设置与您的标签匹配即可。 This might be useful if you wanted to trim at specific places in the label rather than the nearest character / word. 如果您想在标签的特定位置而不是最接近的字符/单词处进行剪裁,这可能会很有用。

Not an expert in WPF, but I would think that you'll need to do this in code rather than XAML. 不是WPF的专家,但我认为您需要使用代码而不是XAML来执行此操作。

Start by obtaining the actual pixel width of the space available for the text. 首先获取文本可用空间的实际像素宽度。 Then look at the character set, dot pitch etc. utilised on the XAML front end and from there calculate the pixel width required per character. 然后查看XAML前端上使用的字符集,点间距等,然后从中计算每个字符所需的像素宽度。

You could also look at changing the character sizes as well as reducing the label length. 您还可以查看更改字符大小以及减少标签长度的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM