* UILine: 修复了文本为空不显示的问题

This commit is contained in:
Sunny 2022-01-10 21:14:10 +08:00
parent 8bccba294d
commit 1e97e2c9de
2 changed files with 56 additions and 45 deletions

Binary file not shown.

View File

@ -18,6 +18,7 @@
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2022-01-05: V3.0.9 线 * 2022-01-05: V3.0.9 线
* 2022-01-10: V3.1.0
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -160,9 +161,7 @@ namespace Sunny.UI
protected override void OnPaintFore(Graphics g, GraphicsPath path) protected override void OnPaintFore(Graphics g, GraphicsPath path)
{ {
if (string.IsNullOrEmpty(Text)) return; SizeF sf = new SizeF(0, 0);
SizeF sf = g.MeasureString(Text, Font);
float x = 0; float x = 0;
Pen pen = new Pen(rectColor, lineSize); Pen pen = new Pen(rectColor, lineSize);
if (LineDashStyle != UILineDashStyle.None) if (LineDashStyle != UILineDashStyle.None)
@ -170,62 +169,75 @@ namespace Sunny.UI
pen.DashStyle = (DashStyle)((int)LineDashStyle); pen.DashStyle = (DashStyle)((int)LineDashStyle);
} }
if (Direction == LineDirection.Horizontal) if (Direction == LineDirection.Horizontal)
{ {
switch (TextAlign) if (Text.IsValid())
{ {
case ContentAlignment.BottomLeft: sf = g.MeasureString(Text, Font);
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height + lineSize) / 2.0f); switch (TextAlign)
break; {
case ContentAlignment.BottomLeft:
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height + lineSize) / 2.0f);
break;
case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleLeft:
x = Padding.Left + TextInterval; x = Padding.Left + TextInterval;
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - sf.Height) / 2); g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - sf.Height) / 2);
break; break;
case ContentAlignment.TopLeft: case ContentAlignment.TopLeft:
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - lineSize) / 2.0f - sf.Height); g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - lineSize) / 2.0f - sf.Height);
break; break;
case ContentAlignment.BottomCenter: case ContentAlignment.BottomCenter:
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height + lineSize) / 2.0f); g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height + lineSize) / 2.0f);
break; break;
case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleCenter:
x = (Width - sf.Width) / 2 - 2; x = (Width - sf.Width) / 2 - 2;
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2); g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2);
break; break;
case ContentAlignment.TopCenter: case ContentAlignment.TopCenter:
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - lineSize) / 2.0f - sf.Height); g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - lineSize) / 2.0f - sf.Height);
break; break;
case ContentAlignment.BottomRight: case ContentAlignment.BottomRight:
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height + lineSize) / 2.0f); g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height + lineSize) / 2.0f);
break; break;
case ContentAlignment.MiddleRight: case ContentAlignment.MiddleRight:
x = Width - sf.Width - TextInterval - 4 - Padding.Right; x = Width - sf.Width - TextInterval - 4 - Padding.Right;
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - sf.Height) / 2); g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - sf.Height) / 2);
break; break;
case ContentAlignment.TopRight: case ContentAlignment.TopRight:
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - lineSize) / 2.0f - sf.Height); g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - lineSize) / 2.0f - sf.Height);
break; break;
}
} }
int top = (Height - lineSize) / 2; int top = (Height - lineSize) / 2;
switch (TextAlign) if (Text.IsValid())
{ {
case ContentAlignment.MiddleLeft: switch (TextAlign)
case ContentAlignment.MiddleCenter: {
case ContentAlignment.MiddleRight: case ContentAlignment.MiddleLeft:
g.DrawLine(pen, Padding.Left, top, x, top); case ContentAlignment.MiddleCenter:
g.DrawLine(pen, x + sf.Width + 2, top, Width - 2 - Padding.Left - Padding.Right, top); case ContentAlignment.MiddleRight:
break; g.DrawLine(pen, Padding.Left, top, x, top);
default: g.DrawLine(pen, x + sf.Width + 2, top, Width - 2 - Padding.Left - Padding.Right, top);
g.DrawLine(pen, Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, top); break;
break; default:
g.DrawLine(pen, Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, top);
break;
}
}
else
{
g.DrawLine(pen, Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, top);
} }
switch (startCap) switch (startCap)
@ -268,7 +280,6 @@ namespace Sunny.UI
} }
pen.Dispose(); pen.Dispose();
} }
UILineDashStyle lineDashStyle = UILineDashStyle.None; UILineDashStyle lineDashStyle = UILineDashStyle.None;