设置Xamarin.Forms标签的字体行高

时间:2015-04-01 16:02:12

标签: xamarin.forms

我有一个Xamarin.Forms标签显示几行文字,我想增加行高,使文字更具可读性。

Label控件和Span控件上可用的字体属性似乎仅限于字体,大小和样式。

如何更改线高?

3 个答案:

答案 0 :(得分:3)

您需要一个可以从现有控件继承的自定义控件和渲染,并处理其他属性。至于iOS(关于Android的idk)你可以使用

// this goes in the Forms project
public class CustomLabel : Label{
  // make it bindable, shortened for simplicity here
  public double LineHeight {get;set;}
}

// this goes in the iOS project, another one in the Android project
// notice the attribute is before the namespace
[assembly: ExportRendererAttribute (typeof(CustomLabel), typeof(CustomLabelRenderer))]
namespace MyNamespace{
public class CustomLabelRenderer: LabelRenderer
  protected override void OnElementChanged (ElementChangedEventArgs<Frame> e){
    base.OnElementChanged(e);
  // sample only; expand, validate and handle edge cases as needed
    ((UILabel)base.Control).Font.LineHeight = ((CustomLabel)this.Element).LineHeight;
  }

  // if your property is bindable you can handle changes in this method:
  // protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
}

就iOS而言(如果Android具有该功能,则为idk)您还可以使用UILabel的UIAppearance来增加整个应用中的行高,具体如下:

UILabel.Appearance.Font.LineHeight = 20f;

答案 1 :(得分:3)

我可以通过定义如下的自定义渲染器来使其工作。首先,在Forms项目(PCL)中,定义自定义标签并添加LineHeight属性:

// Put this your forms project
public class CustomLabel : Label
{
    public double LineHeight { get; set; }
}

然后,在iOS项目中,您可以像这样定义渲染器:

// Put this in your iOS project
[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRenderer))]
namespace MyNamespace
{ 
    public class CustomLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            // Make sure control is not null
            var label = (CustomLabel)Element;
            if (label == null || Control == null)
            {
                return;
            }

            var labelString = new NSMutableAttributedString(label.Text);
            var paragraphStyle = new NSMutableParagraphStyle { LineSpacing = (nfloat)label.LineHeight };
            var style = UIStringAttributeKey.ParagraphStyle;
            var range = new NSRange(0, labelString.Length);

            labelString.AddAttribute(style, paragraphStyle, range);
            Control.AttributedText = labelString;
        }
    }
}

最后,您可以在页面上使用它,如下所示:

new Label {
    Text = "some text",
    LineHeight = 20
}

答案 2 :(得分:0)

我尝试了上述解决方案,但没有成功。在Android和iOS中,行为是不同的,因此我研究了在两个平台上都实现相同行为的类似解决方案。

// Put this your forms project
public class CustomLabel : Label
{
    public static readonly BindableProperty LineHeightProperty =
        BindableProperty.Create("LineHeight", typeof(double), typeof(CustomLabel));

    public double LineHeight
    {
        get { return (double)GetValue(LineHeightProperty); }
        set { SetValue(LineHeightProperty, value); }
    }
}

然后是iOS渲染器:

// Put this in your iOS project
[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRenderer))]
namespace App.iOS.Components
{
    public class CustomLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            var label = (CustomLabel)Element;
            if (label == null || Control == null)
            {
                return;
            }
            double fontSize = label.FontSize;

            //iOS LineSpacing is measured in dp, as the FontSize. So, if we need a LineSpacing of 2, 
            //this LineSpacing must be set to the same value of the FontSize
            nfloat lineSpacing = ((nfloat)label.LineHeight * (nfloat)fontSize) - (nfloat)fontSize;
            if (lineSpacing > 0)
            {
                var labelString = new NSMutableAttributedString(label.Text);
                var paragraphStyle = new NSMutableParagraphStyle { LineSpacing = lineSpacing };
                var style = UIStringAttributeKey.ParagraphStyle;
                var range = new NSRange(0, labelString.Length);

                labelString.AddAttribute(style, paragraphStyle, range);
                Control.AttributedText = labelString;
            }
        }
    }
}

请注意,在iOS中,LineSpacing属性与Android不同: Android中的LineSpacing = 2等同于iOS中的LineSpacing = FontSize

最后,AndroidRenderer:

// Put this in your Android project
[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRenderer))]
namespace App.Droid.Components
{
    public class CustomLabelRenderer : LabelRenderer
    {
        protected CustomLabel CustomLabel { get; private set; }
        public CustomLabelRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                this.CustomLabel = (CustomLabel)this.Element;
            }
            double lineSpacing = this.CustomLabel.LineHeight;
            if (lineSpacing > 0)
            {
                this.Control.SetLineSpacing(1f, (float)lineSpacing);
                this.UpdateLayout();
            }
        }
    }
}

希望它能起作用