TextBlock as big as a capital letter (ignoring font ascender/descender)

前端 未结 2 1819
北恋
北恋 2021-01-02 18:44

I am looking to get a specific behavior on TextBlock so that its height only includes the height of the capital letters (from baseline to top minus \"ascender h

2条回答
  •  离开以前
    2021-01-02 19:17

    u can try to use attribute LineStackingStrategy="BlockLineHeight" and a Converter on the LineHeight attributes and a converter on the Height of TextBlock. This a sample code of converters

    // Height Converter
    public class FontSizeToHeightConverter : IValueConverter
    {
        public static double COEFF = 0.715;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (double)value * COEFF;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    // LineHeightConverter
    public class FontSizeToLineHeightConverter : IValueConverter
    {
        public static double COEFF = 0.875;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return double.Parse(value.ToString()) * COEFF;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    The Coefficient used on converters depends on Used Family Fonts (Baseline and LineSpacing):

    
    

    sample with params Coeff = 0.7

    The best solution is to find how to calculate the Coeff based on parameters Baseline and LineSpacing of the FontFamily. In this sample (Segeo UI) the Coeff of Height = 0.715 and LineHeight = 0,875 * FontSize.

提交回复
热议问题