1. 程式人生 > >wpf中控制元件字型大小智慧隨著內容長度的變化也變化。

wpf中控制元件字型大小智慧隨著內容長度的變化也變化。

在最近的工作中遇到一個需求,控制元件的fontsize 隨著 content的內容變化而自動適應大小。

整理了一下, 首先是2個方法。

        /// <summary>
        /// Measures the size of the text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontStyle">The font style.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <param name="fontStretch">The font stretch.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <returns></returns>
        private static Size MeasureTextSize(
            string text, FontFamily fontFamily,
            FontStyle fontStyle,
            FontWeight fontWeight,
            FontStretch fontStretch,
            double fontSize)
        {
            FormattedText ft = new FormattedText(text,
                                                 CultureInfo.CurrentCulture,
                                                 System.Windows.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>
        private 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);
        }

用來返回size 的大小/

   /// <summary>
        /// Limits the size of the control font.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontStyle">The font style.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <param name="fontStretch">The font stretch.</param>
        /// <param name="fontSize">Size of the font.</param>
        private static void LimitControlFontSize(
            string text,
            FontFamily fontFamily,
            FontStyle fontStyle,
            FontWeight fontWeight,
            FontStretch fontStretch,
            double fontSize, double width)
        {
            for (int i = Convert.ToInt32(fontSize); i > MinFontSize; i--)
            {
                Size size = MeasureText(text, fontFamily, fontStyle,
                    fontWeight, fontStretch, i);


                if (width > size.Width)
                {
                    lastFontSize = i-1;
                    return;
                }
            }


            lastFontSize = MinFontSize;
            LastToolTipContent = text;


            for (int i = text.Length; i > 0; i--)
            {
                var textTemp = text.Substring(0, i);
                Size size = MeasureText(textTemp, fontFamily, fontStyle,
                    fontWeight, fontStretch, lastFontSize);


                if (width > size.Width + 10)
                {
                    lastControlContent = textTemp + "...";
                    return;
                }
            }
        }

用來控制大小。