在我的Xamarin.Forms项目中,我使用MultiLineLabel在1或2行显示标题,具体取决于文本长度.我基于这个博客来实现这一目标.
所以我有一个MultiLineLabel控件:
public class MultiLineLabel : Label
{
private static int _defaultLineSetting = -1;
public static readonly BindableProperty LinesProperty = BindableProperty.Create(nameof(Lines), typeof(int), typeof(MultiLineLabel), _defaultLineSetting);
public int Lines
{
get { return (int)GetValue(LinesProperty); }
set { SetValue(LinesProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
我使用2个渲染器:
在iOS上,我保留了给定的渲染器:
public class CustomMultiLineLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
MultiLineLabel multiLineLabel = (MultiLineLabel)Element;
if (multiLineLabel != null && multiLineLabel.Lines != -1)
Control.Lines = multiLineLabel.Lines; …Run Code Online (Sandbox Code Playgroud)