簡單地擴展Label
有兩個可綁定屬性 - FontSizeFactor
和NamedFontSize
- 並讓他們計算出的字體大小爲您提供:
public class MyLabel : Label
{
public static readonly BindableProperty FontSizeFactorProperty =
BindableProperty.Create(
"FontSizeFactor", typeof(double), typeof(MyLabel),
defaultValue: 1.0, propertyChanged: OnFontSizeFactorChanged);
public double FontSizeFactor
{
get { return (double)GetValue(FontSizeFactorProperty); }
set { SetValue(FontSizeFactorProperty, value); }
}
private static void OnFontSizeFactorChanged(BindableObject bindable, object oldValue, object newValue)
{
((MyLabel)bindable).OnFontSizeChangedImpl();
}
public static readonly BindableProperty NamedFontSizeProperty =
BindableProperty.Create(
"NamedFontSize", typeof(NamedSize), typeof(MyLabel),
defaultValue: NamedSize.Small, propertyChanged: OnNamedFontSizeChanged);
public NamedSize NamedFontSize
{
get { return (NamedSize)GetValue(NamedFontSizeProperty); }
set { SetValue(NamedFontSizeProperty, value); }
}
private static void OnNamedFontSizeChanged(BindableObject bindable, object oldValue, object newValue)
{
((MyLabel)bindable).OnFontSizeChangedImpl();
}
protected virtual void OnFontSizeChangedImpl()
{
if (this.FontSizeFactor != 1)
this.FontSize = (this.FontSizeFactor * Device.GetNamedSize(NamedFontSize, typeof(Label)));
}
}
用法示例:
<Label FontSize="Large" Text="Large Size" />
<local:MyLabel NamedFontSize="Large" FontSizeFactor="0.9" Text="90% Large Size" />
<Label FontSize="Medium" Text="Medium Size" />
<local:MyLabel NamedFontSize="Medium" FontSizeFactor="0.75" Text="75% Medium Size" />
<Label FontSize="Micro" Text="Micro Size" />
<local:MyLabel NamedFontSize="Micro" FontSizeFactor="0.5" Text="50% Micro Size" />
來源
2017-08-24 15:03:52
Ada
Micro是最小的可讀的字體大小,做這個大小的一半,你將無法正確閱讀它,你確定你想這樣嗎? –
問題是: 在我的ios上的佈局微尺寸是大kkkk大很大...然後,xamarin給我的尺寸不太好... –