-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControlSizeUtils.cs
74 lines (66 loc) · 2.63 KB
/
ControlSizeUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
namespace Painter
{
/// <summary>
/// 计算指定字符串占用的高宽
/// </summary>
public class ControlSizeUtils
{
private static Size MeasureTextSize(string text, Typeface typeface, double fontSize)
{
var ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
return new Size(ft.Width, ft.Height);
}
/// <summary>
/// 衡量字符尺寸
/// </summary>
/// <returns></returns>
public static Size GetTextAreaSize(string text, double fontsize)
{
FontFamily fontFamily; FontStyle fontStyle; FontWeight fontWeight; FontStretch fontStretch;
fontFamily = new FontFamily("微软雅黑"); fontStyle = FontStyles.Normal;
fontWeight = FontWeights.Normal;
fontStretch = FontStretches.Normal;
double fontSize = fontsize;
if (text == null)
return new Size(0, 0);
Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
GlyphTypeface gt;
if (!typeface.TryGetGlyphTypeface(out gt))
return MeasureTextSize(text, typeface, fontSize);
double totalWidth = 0;
double totalHeight = 0;
// 替换换行符
var array = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (var splitted in array)
{
double lineWidth = 0;
double lineHeight = 0;
// 计算每行的宽度和高度
for (int n = 0; n < splitted.Length; n++)
{
try
{
ushort glyphIndex = gt.CharacterToGlyphMap[splitted[n]];
double width = gt.AdvanceWidths[glyphIndex] * fontSize;
double height = gt.AdvanceHeights[glyphIndex] * fontSize;
lineHeight = Math.Max(totalHeight, height);
lineWidth += width;
}
catch (Exception exp)
{
lineWidth += fontSize;
}
}
totalWidth = Math.Max(totalWidth, lineWidth);
totalHeight = Math.Max(totalHeight, lineHeight);
}
if (totalWidth < 5)
totalWidth = 5;
return new Size(totalWidth, totalHeight);
}
}
}