首页 > 其他分享 >将单行文字自动适应到目标矩形框内

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

时间:2023-02-27 21:07:58浏览次数:41  
标签:return RectangleF destRectF 矩形框 单行 自动 ._ new font


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);

标签:return,RectangleF,destRectF,矩形框,单行,自动,._,new,font
From: https://blog.51cto.com/JohnsonJu/6089158

相关文章

  • UI自动化
    UI自动化测试   只能做B/S架构项目的自动化测试1.安装 importosimporttimefromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportSer......
  • web自动化--等待
    一、常用知识:一、强制等待time.sleep(3)二、隐式等待语法:driver.implicitly_wait(s)1、整个会话只执行一次,全局起作用,后面的每一次元素查找都会进行等待,在设置的时......
  • 自动化测试环境的搭建 及 识别元素的方法
    自动化测试环境的搭建:一、安装selenium:安装方式一:       pipinstall-Uselenium 安装方式二:   手动安装selenium:      ......
  • 如何使用jQuery自动滚动到页面底部?
      这个$(document).scrollTop($(document).height());  <!DOCTYPEhtml><html><head><title>ScrollAutomatically</title><scriptsrc="https......
  • 浅谈前端自动化构建(Grunt、gulp。webpack)
    前言 现在的前端开发已经不再仅仅只是静态网页的开发了,日新月异的前端技术已经让前端代码的逻辑和交互效果越来越复杂,更加的不易于管理,模块化开发和预处理框架把项目......
  • 上传gitlab代码后jenkins自动进行发布的配置
     1、安装​​GitLabPlugin​​​和​​GenericWebhookTriggerPlugin​​两个插件2、要在gitlab生成一个访问api的token 3、在jenkins的系统管理里找到下面界面进行输......
  • 直播软件搭建,输入框回车以后自动刷新页面
     直播软件搭建,输入框回车以后自动刷新页面可以在标签上添加@submit.native.prevent。 <el-formlabel-position="top"size="mini":inline="true":disabled="disab......
  • OpenHarmony应用签名 - DevEco Studio 自动签名
    概述文档环境开发环境:MacBookProDevEcoStudio版本:DevEcoStudio3.1Beta1(3.1.0.200)SDK版本:3.2.10.6开发板型号:DAYU200系统版本:OpenHarmony3.2Beta5......
  • C# 扩展Dictionary的自动获取方法。
    publicstaticstringget_val_by_key(thisDictionary<string,string>dictionary,stringkey)=>dictionary==null||!dictionary.ContainsKey(key)?"":dicti......
  • 特斯拉自动驾驶算法和模型解读
    特斯拉自动驾驶算法和模型解读特斯拉是一个典型的AI公司,过去一年训练了75000个神经网络,意味着每8分钟就要出一个新的模型,共有281个模型用到了特斯拉的车上。接下来我们分......