首页 > 代码库 > 获取文字长度

获取文字长度

wpf

//获取文字长度
private double MeasureTextWidth(string text, double fontSize, string fontFamily)
{
FormattedText formattedText = new FormattedText(
text,
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily.ToString()),
fontSize,
Brushes.Black
);
return formattedText.WidthIncludingTrailingWhitespace;
}

 

winform

    System.Drawing.Size fontSize = System.Windows.Forms.TextRenderer.MeasureText(value, new System.Drawing.Font("Arial", (float)btnDisplay.FontSize));

获取文字长度