Question

I'd like to find out the baseline of a font in WinRT.

I've also calculated the size of text for a particular font by creating a dummy TextBlock, but I'm not sure how to calculate the baseline. Is this even possible in WinRT?

Was it helpful?

Solution

Unfortunately, what you are looking for is FormattedText [MSDN: 1 2 ] that exists in WPF and doesn't exist in WinRT (i don't even think it's even in silverlight yet).

It will probably be included in a future version because it seems to be a very sought after feature, is sorely missed and the team is aware of it omission. See here: http://social.msdn.microsoft.com.

If you are interested or are really, really in need of a way to measure the specifics of a type face you can try writing a wrapper for DirectWrite which to my knowledge is inside the WinRT available technology stack, however it is only accessible via C++

here is a couple of jumping off points for you, if you want to try:

hope this helps, good luck -ck

update

I thought about this for a bit and remembered that TextBlocks have the oft forgotten property BaselineOffset which gives you the baseline drop from the top of the box for the selected type face! So you can use the same hack everyone is using to replace MeasureString to replace a loss of FormattedText. Here the sauce:

    private double GetBaselineOffset(double size, FontFamily family = null, FontWeight? weight = null, FontStyle? style = null, FontStretch? stretch = null)
    {
        var temp = new TextBlock();
        temp.FontSize = size;
        temp.FontFamily = family ?? temp.FontFamily;
        temp.FontStretch = stretch ?? temp.FontStretch;
        temp.FontStyle = style ?? temp.FontStyle;
        temp.FontWeight = weight ?? temp.FontWeight;

        var _size = new Size(10000, 10000);
        var location = new Point(0, 0);

        temp.Measure(_size);
        temp.Arrange(new Rect(location, _size));

        return temp.BaselineOffset;
    }

and i used that to do this: screenshot

perfect! right? hope this helps -ck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top