[Xamarin平台上的入门者,很好奇为什么我的Label只包装最后两个单词而不是最后一个单词。
我已经将Xamarin的Label
与LineBreakMode="WordWrap"
一起使用,下面将描述结果视图:
您可以看到,最后两个单词被换行,而不仅仅是最后一个单词。另外,它在Android上也能完美运行,只有iOS版本有此问题。
<StackLayout VerticalOptions="StartAndExpand" Padding="{StaticResource MediumSpacingAll}">
<Label Text="Thank you for joining..." TextColor="{StaticResource PrimaryColor}" LineBreakMode="WordWrap" HorizontalOptions="StartAndExpand" FontSize="40" FontAttributes="Bold"/>
</StackLayout>
这是父视图内标签尺寸的可视化。
您可以使用Custom Renderer在iOS中设置内容对齐方式。
using System;
using Foundation;
using UIKit;
using xxx;
using xxx.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer(typeof(Label),typeof(MyLabelRenderer))]
namespace xxx.iOS
{
public class MyLabelRenderer:LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
string content = Control.Text;
NSMutableAttributedString attributedString = new NSMutableAttributedString(content);
NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.Alignment = UITextAlignment.Left;
attributedString.AddAttribute(UIStringAttributeKey.ParagraphStyle,paragraphStyle,new NSRange(0,content.Length));
Control.AttributedText = attributedString;
}
}
}
}