Xamarin.forms 如何使用Xamarin.Android在TextSize和FontSize之间来回转换?

Xamarin.forms 如何使用Xamarin.Android在TextSize和FontSize之间来回转换?,xamarin.forms,xamarin.android,Xamarin.forms,Xamarin.android,我正在使用DisplayMetrics和FontMetrics计算TextSize的值,以便按钮内的文本不大于按钮边界框 DisplayMetrics displayMetrics = new DisplayMetrics(); Display.GetMetrics(displayMetrics); float scaledDensity = displayMetrics.ScaledDensity; TextPaint paint = new TextPaint(); paint.AntiA

我正在使用
DisplayMetrics
FontMetrics
计算
TextSize
的值,以便
按钮
内的文本不大于按钮边界框

DisplayMetrics displayMetrics = new DisplayMetrics();
Display.GetMetrics(displayMetrics);
float scaledDensity = displayMetrics.ScaledDensity;

TextPaint paint = new TextPaint();
paint.AntiAlias = true;
Paint.FontMetrics metrics = null;

Xamarin.Forms.Button button;
MyButtonRenderer renderer = ((MyButtonRenderer)Platform.GetRenderer(button));
Android.Widget.Button control = renderer.Control;

int maxFontSize = (int)renderer.defaultFontSize;
string text = button.Text;
Rect bounds = new Rect();

while (maxFontSize >= 0)
{
    paint.SetTypeface(control.Typeface);
    paint.TextSize = scaledDensity * maxFontSize;
    paint.GetTextBounds(text, 0, text.Length, bounds);
    metrics = paint.GetFontMetrics();
    if (bounds.Width() < control.MeasuredWidth && (metrics.Bottom - metrics.Top) < control.MeasuredHeight)
    {
        break;
    }
    maxFontSize--;
}
但它不起作用,我要做的是:

float conversionScale = (float)renderer.Element.FontSize / renderer.Control.TextSize;
float maxTextSize = maxFontSize * scaledDensity;
renderer.Element.FontSize = maxTextSize * conversionScale;
其中,
conversionScale
是我用来将
TextSize
转换为
FontSize

有没有一种方法可以从API中获取该转换因子,而不是从两个属性的商中推断其值


.

@JoeLv感谢您提供的链接,这些文档与Xamarin中的字体相关。不过表单没有指定如何进行转换。也许我应该看看源代码,幸好Xamarin是FOSS:)
float conversionScale = (float)renderer.Element.FontSize / renderer.Control.TextSize;
float maxTextSize = maxFontSize * scaledDensity;
renderer.Element.FontSize = maxTextSize * conversionScale;