当前位置:网站首页>Automatically fit single line text into the target rectangle
Automatically fit single line text into the target rectangle
2022-04-23 07:54:00 【Dake mountain man】
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace JohnsonJu.CodeSystem.Helper
{
/// <summary>
/// Automatically fit single line text into the target rectangle
/// </summary>
public class FittingSingleLineWordsInRectF
{
string _words;
Font _fontWords;
RectangleF _destRectF = RectangleF.Empty;
/// <summary>
/// Gets or sets the text to automatically fit
/// </summary>
public string Words
{
get { return this._words; }
set { this._words = value; }
}
/// <summary>
/// Gets or sets the font of the text
/// </summary>
public System.Drawing.Font FontWords
{
get { return this._fontWords; }
set { this._fontWords = value; }
}
/// <summary>
/// Gets or sets the destination rectangle
/// </summary>
public System.Drawing.RectangleF DestRectF
{
get { return this._destRectF; }
set { this._destRectF = value; }
}
/// <summary>
/// Constructors
/// </summary>
public FittingSingleLineWordsInRectF()
{
}
/// <summary>
/// Automatically fit single line text to the constructor in the target rectangle
/// </summary>
/// <param name="words"> One line text </param>
/// <param name="font"> The font of the text </param>
/// <param name="destRectF"> Target rectangle </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>
/// Get text path
/// </summary>
/// <param name="words"> written words </param>
/// <param name="font"> typeface </param>
/// <returns> Graphic path </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>
/// Get the return graph path
/// </summary>
/// <returns></returns>
public GraphicsPath RetrievePath()
{
return RetrievePath(this._destRectF, this.RetrieveWordsPath());
}
/// <summary>
/// Automatically adapt the drawing path to the target rectangular area
/// </summary>
/// <param name="destRectF"> Target rectangular area </param>
/// <param name="gp"> The original graphics path </param>
/// <returns> Automatically adapted graphics path </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>
/// Measure the rectangular area occupied by single line text
/// </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);
// Draw text to Sketchpad
// g.DrawString(measureString, stringFont, Brushes.Black, 0, 0, stringFormat);
// measurement
Region[] stringRegions = new Region[1];
stringRegions = g.MeasureCharacterRanges(measureString, stringFont, layoutRect, stringFormat);
// Draw the rectangle for the first measurement
RectangleF measureRect = stringRegions[0].GetBounds(g);
//g.DrawRectangle( new Pen(Color.Red, 1), Rectangle.Round(measureRect1));
return measureRect;
}
}
}
Calling method :
// rect: Your rectangle ( Area ) text: Your text font: Text font g: Your drawing board (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);
版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378488.html
边栏推荐
- Unity获取真实地理地图应用Terrain笔记
- Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
- 颜色转换公式大全及转换表格(31种)
- Unity gets a resource that is referenced by those resources
- Scrapy modifies the time in the statistics at the end of the crawler as the current system time
- MySQL8. 0 installation / uninstallation tutorial [window10 version]
- unity 屏幕自适应
- C# SmoothProgressBar自定义进度条控件
- c#读取INI文件和向ini文件写入数据
- Dictionary & lt; T1,T2&gt; Sorting problem
猜你喜欢

Understanding the role of individual units in a deep neural networks

VBA appelle SAP RFC pour réaliser la lecture et l'écriture des données

Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments

常用Markdown语法学习

基于NLP的软件安全研究(二)

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)

FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning

Houdini fluid > > particle fluid export to unity note

【NLP笔记】CRF原理初探

VBA calls SAP RFC to read & write data
随机推荐
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
About USB flash drive data prompt raw, need to format, data recovery notes
H5 local storage data sessionstorage, localstorage
Personality charm of high paid it workers
将单行文字自动适应到目标矩形框内
【Unity VFX】VFX特效入门笔记-火花制作
中间人环境mitmproxy搭建
ES6使用递归实现深拷贝
Daily question | fear dominated by reverse linked list
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
第四章 无形资产
Unity gets the resources that a file depends on
NodeJS(二)同步读取文件和异步读取文件
C reads the registry
SVG中年月日相关的表达式
根据某一指定的表名、列名及列值来向前或向后N条查相关列值的SQL自定义标量值函数
Unityshader Foundation
每天工作4小时的程序员
The problem of exporting excel form with wireframe and internal spacing of form by using web form
C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)