2009-05-05 3 views
13

Я использовал TextRenderer для измерения длины строки и, соответственно, для управления размером. Есть ли эквивалент в WPF или я могу просто использовать TextRendered.MeasureString?WPF эквивалент TextRenderer

ответ

3

Посмотрите на FormattedText class

Если вам нужен более детальный контроль, то вам нужно сходить к AdvanceWidths члену типа-GlyphTypeface в. Найден similar discussion здесь с фрагментом кода, который выглядит так, как будто он может работать.

Обновление: Похоже, что это может быть дубликат Measuring text in WPF .. OP подтвердите пожалуйста.

18

Благодаря Gishu,

Чтение ссылок я придумал следующее оба из которых делают работу для меня:

/// <summary> 
    /// Get the required height and width of the specified text. Uses FortammedText 
    /// </summary> 
    public static Size MeasureTextSize(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize) 
    { 
     FormattedText ft = new FormattedText(text, 
              CultureInfo.CurrentCulture, 
              FlowDirection.LeftToRight, 
              new Typeface(fontFamily, fontStyle, fontWeight, fontStretch), 
              fontSize, 
              Brushes.Black); 
     return new Size(ft.Width, ft.Height); 
    } 

    /// <summary> 
    /// Get the required height and width of the specified text. Uses Glyph's 
    /// </summary> 
    public static Size MeasureText(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize) 
    { 
     Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch); 
     GlyphTypeface glyphTypeface; 

     if(!typeface.TryGetGlyphTypeface(out glyphTypeface)) 
     { 
      return MeasureTextSize(text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize); 
     } 

     double totalWidth = 0; 
     double height = 0; 

     for (int n = 0; n < text.Length; n++) 
     { 
      ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]]; 

      double width = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize; 

      double glyphHeight = glyphTypeface.AdvanceHeights[glyphIndex]*fontSize; 

      if(glyphHeight > height) 
      { 
       height = glyphHeight; 
      } 

      totalWidth += width; 
     } 

     return new Size(totalWidth, height); 
    } 
Смежные вопросы