Measure length of text for a label
I'm trying to dynamically shorten a label based on available space. I use FormattedText to determine the length of the 开发者_开发技巧candidate string
formattedText = new FormattedText(candidateString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, FontSize = fontSize, Brushes.Black);
I should be able to retrieve the Typeface from the Label containing the text; however I keep running into roadblocks:
- Label contains FontFamily but not Typeface.
- I can't create a new Typeface from FontFamily since I can't get a single FontFamily name
Typeface t = new Typeface(theLabel.FontFamily.FamilyNames.?, theLabel.FontStyle, theLabel.FontSize, theLabel.FontStretch);
This seems like it ought to be simple: I want the typeface used in THAT label (pointing to screen). How can a create a Typeface for use in measuring my string length? Is there a simpler way to accomplish this? What am I missing here?
Thanks.
This doesn't work?
Typeface t = new Typeface(theLabel.FontFamily,theLabel.FontStyle,theLabel.FontWeight,theLabel.FontStretch, new FontFamily("Arial"));
or you can experiment with
theLabel.FontFamily.GetTypefaces();
精彩评论