Xamarin表格标签 - 对齐?

问题描述 投票:6回答:7

我只是想问一下是否有任何方法可以证明Label中的文本。我正在使用Xamarin Forms Xaml。

谢谢。

更新:至于现在,不可能证明文本的合理性。大多数答案都是关于文本的中心,但这不是我的要求。一种方法是使用渲染器作为提摩太。

xaml xamarin label text-alignment justify
7个回答
3
投票

虽然您无法使用Xamarin.Forms功能将标签的文本拉伸到整个宽度,但使用平台渲染器很容易实现。

Android text justification in Xamarin.Froms label

大多数Xamarin平台都具有相应原生元素中可用的文本对齐功能,而这只是设置本机元素的单个属性的问题。我认为不将此功能添加到标准Xamarin.Forms标签的原因是该功能中的平台滞后,例如Android只在8.1版中添加了Android.Text.JustificationMode.InterWord标志

您可以在下面看到Android渲染器实现:

using Android.Content;
using Saplin.CPDT.UICore.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Saplin.CPDT.UICore.Controls.ExtendedLabel), typeof(Saplin.CPDT.Droid.ExtnededLabelRenderer))]
namespace Saplin.CPDT.Droid
{
    public class ExtnededLabelRenderer : Xamarin.Forms.Platform.Android.LabelRenderer
    {
        public ExtnededLabelRenderer(Context context) : base(context)
        {
        }

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

            var el = (Element as ExtendedLabel);

            if (el != null && el.JustifyText)
            {
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                {
                    Control.JustificationMode = Android.Text.JustificationMode.InterWord;
                }

            }
        }
    }
}
  1. 您在本机项目中创建渲染器类
  2. 您添加程序集:ExportRenderer属性
  3. 您设置TextView的JustificationMode

在我的例子中,我使用Xamarin.Forms.Label的ExtenedLabel子类和额外的属性JustifyText来设置文本的对齐。这就是如何声明子类控件:

using System;
using Xamarin.Forms;

namespace Saplin.CPDT.UICore.Controls
{
    public class ExtendedLabel : Label
    {
        public static readonly BindableProperty JustifyTextProperty =
            BindableProperty.Create(
                propertyName: nameof(JustifyText),
                returnType: typeof(Boolean),
                declaringType: typeof(ExtendedLabel),
                defaultValue: false,
                defaultBindingMode: BindingMode.OneWay
         );

        public bool JustifyText
        {
            get { return (Boolean)GetValue(JustifyTextProperty); }
            set { SetValue(JustifyTextProperty, value); }
        }
    }
}
  • WPFmacOS的平台渲染器示例。

6
投票

目前的方法是使用HorizontalTextAlignmentTextAlignment枚举的值是:

  • Center =中心对齐的文本
  • Start =左对齐
  • End =右对齐

将标签及其文本示例居中:

<Label x:Name="Description" HorizontalTextAlignment="Center" VerticalOptions="Center" HorizontalOptions="Center" />


1
投票

使用XAlign属性

Label lbl = new Label();
lbl.Text = "I'm a Label!";
lbl.XAlign = TextAligntment.Start; // Start, Center, End are valid

0
投票

你用什么样的容器来保存文本?使用具有FillAndExpand的Horizo​​ntalOptions的StackLayout以及XAlign可能会执行此操作,但前提是每个控件的文本只有一行。


0
投票

试试这个:

<StackLayout HorizontalOptions="FillAndExpand" Padding="0, 10, 0, 10" Spacing="0">
      <Label Text="Test message" XAlign="Center"/>
      <Label FontSize="Small" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." LineBreakMode="WordWrap"
XAlign="Center" />
</StackLayout>

0
投票

如果您使用relativeLayout下的Label,您可以证明标签的合理性。

诀窍是你必须填写父母的宽度和高度..

所以我使用HeightConstraint,WidthConstraint和factor = 1 ..因此它需要父级的全宽和高度。

  <RelativeLayout >

    <Label Text="My text"
           FontSize="20" 
           HorizontalOptions="Center" 
           VerticalOptions="Center"
           RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}"
           RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}" />
  </RelativeLayout>

0
投票

因为它不能直接在标签内完成,所以解决方法是使用来自Xamarin.Forms的新FlexLayout 3.想法是将文本拆分为空格字符并在FlexLayout中插入相应的标签,并将JustifyContent设置为SpaceBetween

示例:

XAML

<Frame
    HorizontalOptions="Center"
    Margin="20,50,20,0"
    Padding="10"
    WidthRequest="300"
    HasShadow="true">
    <FlexLayout
        x:Name="TextContainer"
        Direction="Row"
        AlignItems="End"
        JustifyContent="SpaceBetween"
        Wrap="Wrap"/>
</Frame>

代码背后

var textparts = "This is a long text to be justified"
                .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                .Select(x => new Label
                {
                    Text = x,
                    FontSize = 12,
                    TextColor = Color.FromHex("#555555"),
                    Margin = new Thickness(1, 0)
                });

            foreach (var textpart in textparts)
                TextContainer.Children.Add(textpart);
© www.soinside.com 2019 - 2024. All rights reserved.