简体   繁体   中英

WinRT How to Get Pixel Width of given String

There is a solution here:

FormattedText

But Alas, this is not supported in WinRT. Is there a replacement/alternate for this in WinRT?

The following solution is taken from this answer: Calculate Font baseline in WinRT

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;
}

I manage to get around this with a HACK type of solution. My main requirement was to reserve space for some text. I used a fixed font and calculated space required for each "blank space". Once i knew this, the rest was trivial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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