当前位置:网站首页>将单行文字自动适应到目标矩形框内
将单行文字自动适应到目标矩形框内
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
边栏推荐
- 反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
- Thorough inquiry -- understanding and analysis of cocos2d source code
- Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
- 配置npm
- ogldev-读书笔记
- js之作用域、作用域链、全局变量和局部变量
- State synchronization and frame synchronization
- npm 安装踩坑
- SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。
- Super classic & Programming Guide (red and blue book) - Reading Notes
猜你喜欢
移动端布局(3D转换、动画)
自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)
js之DOM学习获取元素
SAP PI/PO功能运行状态监控检查
Mysql 索引
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
ABAP 实现发布RESTful服务供外部调用示例
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
SAP PI/PO登录使用及基本功能简介
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
随机推荐
页面动态显示时间(升级版)
Reflection on the systematic design of Android audio and video caching mechanism
5. Sql99 standard: internal connection and external connection
2022.3.14 阿里笔试
State synchronization and frame synchronization
反思|开启B站少女心模式,探究APP换肤机制的设计与实现
14.事务处理
typescript字典的使用
js案例之求最大值,反转数组,冒泡排序
Thorough inquiry -- understanding and analysis of cocos2d source code
反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
Design optimization of MySQL database
canvas学习第一篇
刨根问底---cocos2d源码的理解与分析
SAP PI/PO功能运行状态监控检查
【自我激励系列】你永远不会准备好
学会使用搜索引擎
Use of typescript dictionary
11. Table and library management
js之DOM事件