Xamarin 有没有办法让标签从左到右填满空间?
以下是我的代码:Xamarin 有没有办法让标签从左到右填满空间?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,以下是我的代码: 第一个多行标签从左侧开始,但在右侧的某些行上有空格。第二个和第三个多行标签居中,左右两侧都有空格 有什么方法可以让所有的标签行从左到右完全填充,每行的第一个字符总是排在左边,每行最后一个单词的最后一个字符总是排在右边?请注意,这需要每行中的一些单词之间有不同的间隙 我想你可以试试 HorizontalOptions=LayoutOptions.FillAndExpand 我从未见过任何简单的解决方案,只有前面提到的解决方法 您需要为每个平台使用组件或创建自己的解决方案。实
第一个多行标签从左侧开始,但在右侧的某些行上有空格。第二个和第三个多行标签居中,左右两侧都有空格
有什么方法可以让所有的标签行从左到右完全填充,每行的第一个字符总是排在左边,每行最后一个单词的最后一个字符总是排在右边?请注意,这需要每行中的一些单词之间有不同的间隙 我想你可以试试
HorizontalOptions=LayoutOptions.FillAndExpand
我从未见过任何简单的解决方案,只有前面提到的解决方法
您需要为每个平台使用组件或创建自己的解决方案。实现标签对齐支持有点棘手,但可以通过平台渲染器实现 第一步是在forms项目中声明自定义控件
public class JustifiedLabel : Label { }
下一步是在iOS中定义并注册平台渲染器。这一个很简单,因为我们只是简单地将格式化字符串与段落样式结合起来,以得到我们想要的
[assembly: ExportRenderer(typeof(JustifiedLabel), typeof(JustifiedLabelRenderer))]
namespace SomeAppNamespace.iOS
{
public class JustifiedLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
//if we have a new forms element, we want to update text with font style (as specified in forms-pcl) on native control
if (e.NewElement != null)
UpdateTextOnControl();
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
//if there is change in text or font-style, trigger update to redraw control
if(e.PropertyName == nameof(Label.Text)
|| e.PropertyName == nameof(Label.FontFamily)
|| e.PropertyName == nameof(Label.FontSize)
|| e.PropertyName == nameof(Label.TextColor)
|| e.PropertyName == nameof(Label.FontAttributes))
{
UpdateTextOnControl();
}
}
void UpdateTextOnControl()
{
if (Control == null)
return;
//define paragraph-style
var style = new NSMutableParagraphStyle()
{
Alignment = UITextAlignment.Justified,
FirstLineHeadIndent = 0.001f,
};
//define attributes that use both paragraph-style, and font-style
var uiAttr = new UIStringAttributes()
{
ParagraphStyle = style,
BaselineOffset = 0,
Font = Control.Font
};
//define frame to ensure justify alignment is applied
Control.Frame = new RectangleF(0, 0, (float)Element.Width, (float)Element.Height);
//set new text with ui-style-attributes to native control (UILabel)
var stringToJustify = Control.Text ?? string.Empty;
var attributedString = new Foundation.NSAttributedString(stringToJustify, uiAttr.Dictionary);
Control.AttributedText = attributedString;
Control.Lines = 0;
}
}
}
样本使用
我很惊讶还没有人提起这件事 实现相同效果的一种非常简单的方法是将标签封装在其自身的StackLayout中,如下所示:
<StackLayout Orientation="Vertical" >
<Label Text="My Label" />
</StackLayout>
也许你是说开始吧,嘿,艾伦!你能发布一张显示你想要的结果的图片吗?非常感谢你的回答。我会检查一下,如果一切顺利,我会接受你的答案。谢谢你花这么多时间帮忙。
<StackLayout Margin="20">
<Entry x:Name="InputEntry" />
<Label Margin="0,10,0,0" BackgroundColor="Navy" TextColor="White" Text="Normal Text Label" FontSize="15" HorizontalOptions="CenterAndExpand" />
<Label
FontSize="20"
FontAttributes="Bold"
Text="{Binding Text, Source={x:Reference InputEntry}}" />
<Label Margin="0,10,0,0" BackgroundColor="Navy" TextColor="White" Text="Justified Text Label" FontSize="15" HorizontalOptions="CenterAndExpand" />
<local:JustifiedLabel
FontSize="20"
FontAttributes="Bold"
Text="{Binding Text, Source={x:Reference InputEntry}}"
TextColor="Green"
BackgroundColor="Yellow"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
</StackLayout>
<StackLayout Orientation="Vertical" >
<Label Text="My Label" />
</StackLayout>