当前位置:网站首页>将单行文字自动适应到目标矩形框内
将单行文字自动适应到目标矩形框内
2022-04-23 06:26:00 【大可山人】
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);
版权声明
本文为[大可山人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/johnsuna/article/details/120382174
边栏推荐
- How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
- Mobile game performance optimization
- Custom time format (yyyy-mm-dd HH: mm: SS week x)
- 6.聚合函数和分组统计
- Super classic & Programming Guide (red and blue book) - Reading Notes
- redis连接出错 ERR AUTH <password> called without any password configured for the default user.
- 面经的总结
- Processing of common dependency module
- ‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
- Mysql 索引
猜你喜欢
随机推荐
12. Constraints
keytool: command not found
State synchronization and frame synchronization
反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
Django uses MySQL database to solve error reporting
Understanding of STL container
Install and configure Taobao image NPM (cnpm)
Two threads print odd and even numbers interactively
new的实现
8. Paging query
keytool: command not found
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
Configure NPM
Simple random roll call lottery (written under JS)
Reflection on the systematic design of Android audio and video caching mechanism
3.排序语句
js之排他思想及案例
Methods of database query optimization
Date对象(js内置对象)
SAP pi / PO rfc2soap publishes RFC interface as WS example