【问题标题】:Xamarin Forms : Display multiple lines of text in grid cellXamarin Forms:在网格单元格中显示多行文本
【发布时间】:2020-02-13 10:43:20
【问题描述】:

如何以多行格式显示标签文本,as-

First Name
Last Name
Date of birth

所有这些都应该出现在网格的单个单元格中。

这是我尝试过的:

 string full_details = firstname + lastname + dob;
 Label name = new Label
 {
   Text = full_details,
   MaxLines = 3,
   LineBreakMode = LineBreakMode.WordWrap,
   VerticalOptions = LayoutOptions.Center
 };
 grid.Children.Add(name,0,0);

【问题讨论】:

  • 您是否已经尝试过 - 如果是,请添加代码
  • 您是否尝试过将Span 用于Label
  • 你可以使用\n这是一个类似的线程或者你可以在C#代码中使用Environment.NewLine。例如:string full_details = "firstname" + Environment.NewLine + " lastname ";。你可以参考一下。 stackoverflow.com/questions/28927167/…

标签: xamarin xamarin.forms


【解决方案1】:

试试这个

string full_details = firstname + "\n" + lastname + "\n" + dob;
Label name = new Label
{
   Text = full_details,
   MaxLines = 3,
   LineBreakMode = LineBreakMode.WordWrap,
   VerticalOptions = LayoutOptions.Center
};
grid.Children.Add(name,0,0);

尝试添加换行符。

对于不同的字体尝试做这样的事情,

    var formattedString = new FormattedString ();
    formattedString.Spans.Add (new Span{ Text = firstname , ForegroundColor = Color.Red, FontAttributes = FontAttributes.Bold });

    var span = new Span { Text = "\n" + lastname };
    formattedString.Spans.Add(span);
    formattedString.Spans.Add (new Span { Text = "\n" +dob, FontAttributes = FontAttributes.Italic, FontSize =  Device.GetNamedSize(NamedSize.Small, typeof(Label)) });

    Label name = new Label 
    {
        FormattedString = formattedString
    };

【讨论】:

  • 是的,这行得通,但是如果我想为每一行添加不同的字体布局怎么办?
  • 您的名字、姓氏和出生日期是局部变量还是 xaml 中占位符的绑定变量?
  • 它们是局部变量
【解决方案2】:

试试这个代码 sn-ps,可能会有所帮助

<Label x:Name="MovAvgLabel" Grid.Column="0" HorizontalOptions="Center" HorizontalTextAlignment="Center" >
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{Binding Path=MovingAverage}" FontSize="Medium" FontAttributes="Bold"/>
            <Span Text="&#10;Moving Average&#10;Today"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2019-09-02
    • 2019-04-04
    • 2011-01-16
    相关资源
    最近更新 更多