我想问一下,在使用Xamarin Forms Xaml的情况下,有没有办法对
更新: 目前还不支持文本对齐。大多数答案都是关于居中文本的,但这不是我要问的。一种方法可以使用Timothy提供的Renderer。
我想问一下,在使用Xamarin Forms Xaml的情况下,有没有办法对
更新: 目前还不支持文本对齐。大多数答案都是关于居中文本的,但这不是我要问的。一种方法可以使用Timothy提供的Renderer。
虽然你不能使用Xamarin.Forms功能将标签的文本拉伸到全宽,但可以通过平台渲染器轻松实现。
大多数Xamarin平台都有相应本地元素中可用的文本对齐功能,只需设置本地元素的一个属性即可。我认为之所以没有将此功能添加到标准的Xamarin.Forms标签中,是因为平台在该功能方面的落后,例如,Android仅在8.1版本中添加了Android.Text.JustificationMode.InterWord标志。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;
}
}
}
}
}
在我的示例中,我使用了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); }
}
}
}
HorizontalTextAlignment
,其中TextAlignment
枚举的值为:
Center
= 居中对齐的文本Start
= 左对齐End
= 右对齐<Label x:Name="Description" HorizontalTextAlignment="Center"
VerticalOptions="Center" HorizontalOptions="Center" />
<Label LineBreakMode="WordWrap" TextType="Html" TextColor="Black">
<Label.Text><p style="text-align:justify;">
Your text here
<p></Label.Text>
XAlign
属性。Label lbl = new Label();
lbl.Text = "I'm a Label!";
lbl.XAlign = TextAligntment.Start; // Start, Center, End are valid
FlexLayout
。其思路是在空格处拆分文本,并在FlexLayout
中插入相应的标签,并将JustifyContent
设置为SpaceBetween
。<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);
自定义标签
using System;
using Xamarin.Forms;
namespace YourNameSpace.Controls
{
public class CustomLabel : Label
{
public static readonly BindableProperty JustifyTextProperty =
BindableProperty.Create(nameof(JustifyText), typeof(bool), typeof(CustomLabel), false, BindingMode.TwoWay);
public bool JustifyText
{
get { return (bool)GetValue(JustifyTextProperty); }
set
{
SetValue(JustifyTextProperty, value);
}
}
public CustomLabel()
{
}
}
}
iOS自定义标签渲染器
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using YourNameSpace.Controls;
using YourNameSpace.iOS.Renderers;
[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRenderer))]
namespace YourNameSpace.iOS.Renderers
{
public class CustomLabelRenderer : Xamarin.Forms.Platform.iOS.LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control == null || this.Element == null) return;
Control.TextAlignment = UITextAlignment.Justified;
}
}
}
Android自定义标签渲染器
using Android.Content;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using YourNameSpace.Controls;
using YourNameSpace.Droid.Renderers;
[assembly: ExportRenderer(typeof(CustomLabel), typeof(CustomLabelRenderer))]
namespace YourNameSpace.Droid.Renderers
{
public class CustomLabelRenderer : Xamarin.Forms.Platform.Android.LabelRenderer
{
public CustomLabelRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var element = (Element as CustomLabel);
if (element != null && element.JustifyText)
{
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
Control.JustificationMode = Android.Text.JustificationMode.InterWord;
}
}
}
}
}
我已经成功地使用嵌套网格实现了这一点。希望这能帮助到某些人!
<Grid HorizontalOptions="Fill" Padding="5,2">
<Grid Margin="8, 85,8,0" VerticalOptions="Center" RowSpacing="0" >
<Label Grid.Row="0" x:Name="PostTitle" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
Text="Display Text" Padding="10,10" LineHeight="20" BackgroundColor="Black" TextColor="WhiteSmoke" />
<Label Grid.Row="1" x:Name="PostDate" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
Text="Your Text Here" FontSize="Micro" Padding="10,10" LineHeight="10"
BackgroundColor="Black" TextColor="WhiteSmoke" />
</Grid>
</Grid>
UIStringAttributes stringAttributes = new UIStringAttributes
{
ParagraphStyle = new NSMutableParagraphStyle() { Alignment = UITextAlignment.Justified }
};
var attributedText = new NSMutableAttributedString(cell.TextLabel.Text);
attributedText.AddAttributes(stringAttributes, new NSRange(0, cell.TextLabel.Text.Length));
cell.TextLabel.AttributedText = attributedText;
试试这个:
<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>