mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-03 00:00:57 +08:00
39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using System.Windows.Media;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner.Helpers
|
|
{
|
|
public class GetTextDisplayWidthHelper
|
|
{
|
|
public static Double GetTextDisplayWidth(Label label)
|
|
{
|
|
|
|
return GetTextDisplayWidth(label.Content.ToString(), label.FontFamily, label.FontStyle, label.FontWeight, label.FontStretch, label.FontSize);
|
|
|
|
}
|
|
|
|
public static Double GetTextDisplayWidth(string str, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize)
|
|
{
|
|
|
|
var formattedText = new FormattedText(
|
|
str,
|
|
CultureInfo.CurrentUICulture,
|
|
FlowDirection.LeftToRight,
|
|
new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
|
|
fontSize,
|
|
Brushes.Black
|
|
);
|
|
|
|
Size size = new Size(formattedText.Width, formattedText.Height);
|
|
|
|
return size.Width;
|
|
|
|
}
|
|
}
|
|
}
|