Skip to content

Instantly share code, notes, and snippets.

@SamedBll
Created October 6, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamedBll/5f213da8e1dbfebbf3b598dece268340 to your computer and use it in GitHub Desktop.
Save SamedBll/5f213da8e1dbfebbf3b598dece268340 to your computer and use it in GitHub Desktop.
public class EmpiricalFontSizePage : ContentPage
{
Label label;
public EmpiricalFontSizePage()
{
label = new Label();
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
ContentView contentView = new ContentView
{
Content = label
};
contentView.SizeChanged += OnContentViewSizeChanged;
Content = contentView;
}
void OnContentViewSizeChanged(object sender, EventArgs args)
{
// Boyutu değişen Görünümü Al.
View view = (View)sender;
if (view.Width <= 0 || view.Height <= 0)
return;
label.Text =
"This is a paragraph of text displayed with " +
"a FontSize value of ?? that is empirically " +
"calculated in a loop within the SizeChanged " +
"handler of the Label's container. This technique " +
"can be tricky: You don't want to get into " +
"an infinite loop by triggering a layout pass " +
"with every calculation. Does it work?";
Bu, bir FontSize değeri ile görüntülenen metnin bir paragrafıdır ?? Ampirik (deneysel) olarak Label'ın Konteynerinin SizeChanged handlerı içindeki bir döngü içinde hesaplanır. Bu teknik zor olabilir: Her hesaplamada bir düzen geçişi tetikleyerek sonsuz bir döngüye girmek istemezsiniz. Çalışıyor mu?
// İşlenmiş metnin yüksekliğini hesaplayın.
FontCalc lowerFontCalc = new FontCalc(label, 10, view.Width);
FontCalc upperFontCalc = new FontCalc(label, 100, view.Width);
while (upperFontCalc.FontSize - lowerFontCalc.FontSize > 1)
{
// Üst ve alt sınırların ortalama yazı tipi boyutunu elde edin.
double fontSize = (lowerFontCalc.FontSize + upperFontCalc.FontSize) / 2;
// Yeni metin yüksekliğini konteynırın yüksekliğine göre kontrol edin.
FontCalc newFontCalc = new FontCalc(label, fontSize, view.Width);
if (newFontCalc.TextHeight > view.Height)
{
upperFontCalc = newFontCalc;
}
else
{
lowerFontCalc = newFontCalc;
}
}
// Son yazı tipi boyutunu ve gömülü (embedded) değeri olan metni ayarlayın.
label.FontSize = lowerFontCalc.FontSize;
label.Text = label.Text.Replace("??", label.FontSize.ToString("F0"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment