将单行文字自动适应到目标矩形框内

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace JohnsonJu.CodeSystem.Helper
{
    /// <summary>
    /// 将单行文字自动适应到目标矩形框内
    /// </summary>
    public class FittingSingleLineWordsInRectF
    {
        string _words;
        Font _fontWords;
        RectangleF _destRectF = RectangleF.Empty;

        /// <summary>
        /// 获取或设置要自动适应的文字
        /// </summary>
        public string Words
        {
            get { return this._words; }
            set { this._words = value; }
        }

        /// <summary>
        /// 获取或设置文字的字体
        /// </summary>
        public System.Drawing.Font FontWords
        {
            get { return this._fontWords; }
            set { this._fontWords = value; }
        }

        /// <summary>
        /// 获取或设置目标矩形区
        /// </summary>
        public System.Drawing.RectangleF DestRectF
        {
            get { return this._destRectF; }
            set { this._destRectF = value; }
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public FittingSingleLineWordsInRectF()
        {
        }

        /// <summary>
        /// 将单行文字自动适应到目标矩形框内的构造器
        /// </summary>
        /// <param name="words">单行文字</param>
        /// <param name="font">文字的字体</param>
        /// <param name="destRectF">目标矩形区</param>
        public FittingSingleLineWordsInRectF(string words, Font font, RectangleF destRectF)
        {
            this._words = words;
            this._fontWords = font;
            this._destRectF = destRectF;
        }

        private GraphicsPath RetrieveWordsPath()
        {
            return this.RetrieveWordsPath(this._words, this._fontWords);
        }

        /// <summary>
        /// 获取文字路径
        /// </summary>
        /// <param name="words">文字</param>
        /// <param name="font">字体</param>
        /// <returns>图形路径</returns>
        private GraphicsPath RetrieveWordsPath(string words, Font font)
        {
            Rectangle rect = new Rectangle(0, 0, 9999, font.Height);
            GraphicsPath gp = new GraphicsPath();
            gp.AddString(words, font.FontFamily, (int)font.Style, font.Size, rect, null);
            return gp;
        }

        /// <summary>
        /// 得到返回图形路径
        /// </summary>
        /// <returns></returns>
        public GraphicsPath RetrievePath()
        {
            return RetrievePath(this._destRectF, this.RetrieveWordsPath());
        }

        /// <summary>
        /// 将图形路径自动适应到目标矩形区域
        /// </summary>
        /// <param name="destRectF">目标矩形区域</param>
        /// <param name="gp">原来的图形路径</param>
        /// <returns>自动适应后的图形路径</returns>
        public GraphicsPath RetrievePath(RectangleF destRectF, GraphicsPath gp)
        {
            if (destRectF == RectangleF.Empty) return null;

            PointF[] destPoints = new PointF[]{
                                                  new PointF(destRectF.Left, destRectF.Top),
                                                  new PointF(destRectF.Right, destRectF.Top),
                                                  new PointF(destRectF.Left, destRectF.Bottom),
                                                  new PointF(destRectF.Right, destRectF.Bottom)
                                              };
            RectangleF srcRect = gp.GetBounds();
            gp.Warp(destPoints, srcRect);
            GraphicsPath gpNew = (GraphicsPath)gp.Clone();
            return gpNew;
        }

        public RectangleF MeasureTextRectangle(Graphics g, string measureString, Font font)
        {
            SizeF sizeF = g.MeasureString(measureString, font);
            RectangleF rectF = new RectangleF(new PointF(0, 0), sizeF);

            return rectF;
        }

        /// <summary>
        /// 测量单行文本所占的矩形区域
        /// </summary>
        /// <param name="g"></param>
        /// <param name="measureString"></param>
        /// <param name="stringFont"></param>
        /// <returns></returns>
        public RectangleF MeasureCharacterRangesRegions(Graphics g, string measureString, Font stringFont)
        {
            CharacterRange[] characterRanges ={
                                                  new CharacterRange(0, measureString.Length)
                                              };
            float width = 99999F;
            float height = stringFont.Height;
            RectangleF layoutRect = new RectangleF(0, 0, width, height);
            StringFormat stringFormat = new StringFormat();
            //stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            stringFormat.SetMeasurableCharacterRanges(characterRanges);
            // 绘制文字到画板
            //    g.DrawString(measureString, stringFont, Brushes.Black, 0, 0, stringFormat);
            // 测量
            Region[] stringRegions = new Region[1];
            stringRegions = g.MeasureCharacterRanges(measureString, stringFont, layoutRect, stringFormat);
            //绘制首次测量的矩形
            RectangleF measureRect = stringRegions[0].GetBounds(g);
            //g.DrawRectangle( new Pen(Color.Red, 1),    Rectangle.Round(measureRect1));
            return measureRect;
        }
    }
}

调用方法:

// rect: 您的矩形(区域) text: 您的文字  font:文字字体  g:您的画板(Graphics)

FittingSingleLineWordsInRectF fsw = new FittingSingleLineWordsInRectF();
fsw.DestRectF = new RectangleF(rect.X, rect.Y, rect, rect);
fsw.Words = text;
fsw.FontWords = font;
GraphicsPath gpText =fsw.RetrievePath();
g.FillPath(brushForColorBar, gpText);

猜你喜欢

转载自blog.csdn.net/johnsuna/article/details/120382174