C# 如何连续制作两个标签?第二个标签是否正确显示新行?
如何连续制作两个标签,使屏幕上标签的第二个末端将剩余文本移动到新行上 例如。第一个标签——“我知道了,带我去看电影” 第二个标签-“主屏幕”C# 如何连续制作两个标签?第二个标签是否正确显示新行?,c#,xamarin,xamarin.forms,labels,C#,Xamarin,Xamarin.forms,Labels,如何连续制作两个标签,使屏幕上标签的第二个末端将剩余文本移动到新行上 例如。第一个标签——“我知道了,带我去看电影” 第二个标签-“主屏幕” Like@Quantic说,您可以简单地在文本中添加一行,如: Label.Text = "I got it. Take me to the" + Environment.NewLine + "Home Screen"; 这与执行以下操作相同: Label.Text = "I got it. Take me to the\nHome Screen";
Like@Quantic说,您可以简单地在文本中添加一行,如:
Label.Text = "I got it. Take me to the" + Environment.NewLine + "Home Screen";
这与执行以下操作相同:
Label.Text = "I got it. Take me to the\nHome Screen";
或使用堆叠布局:
StackLayout layout = new StackLayout {
Orientation = StackOrientation.Horizontal,
Children = {
new Label { Text = "I got it. Take me to the" },
new Label { Text = "Home Screen" },
}
}
尽管如此,您可能不想这样做,因为不同的屏幕将有不同的宽度,所以您需要考虑到这一点。不同的设备也可以更改应用程序中的文本大小,这样也会影响大小
*编辑:如果希望标签的末尾截断文本,则可以设置标签的LineBreakMode
,如下所示:
Label.LineBreakMode = LineBreakMode.TailTruncation
有时,根据标签
所包含的版面类型,此操作不起作用,因此如果它不起作用,请告诉我您存储标签
的版面类型
因此,如果在StackLayout
中有标签
,则截断可能无法在Android上正常工作。尝试将标签
放在网格中。遇到此问题的其他人请参阅帖子。您将无法让两个标签彼此相邻,包装方式与示例中一样,需要非矩形边界框和Xamarin。表单(或我知道的任何其他UIKit)不支持此功能
但您可以使用具有多个跨距的单个标签
:
var formattedString=new formattedString();
formattedString.Span.Add(新Span{BackgroundColor=Color.Red,ForegroundColor=Color.Olive,Text=“我找到了。带我到”});
formattedString.Spans.Add(新Span{BackgroundColor=Color.Black,ForegroundColor=Color.White,Text=“Home Screen.”,FontAttributes=FontAttributes.Bold});
变量标签=新标签{
FormattedText=formattedString
};
您希望您的标签
看起来像图像,还是图像显示了它当前的样子,而您希望它看起来不同?您能给我们看一下代码吗?我不知道xamarin或这些字符串是如何进入标签的,但对于单个字符串,您可以使用environment.NewLine
,输入您环境的“新行”字符,例如:“我知道了。先带我到“+environment.NewLine+”主屏幕“
”。如果您将被包装在stacklayout(方向水平)中,屏幕单词“screen”的宽度将减小为单词fall“Home”而不是我想要的,则看起来像图像。在建筑红线中断模式下,通过“Hello”@IgorStrekha删除“Screen”一词,以便在转到新线时不显示“Screen”一词?有点像图片中的标题?我不想第一个和第二个标签尾部树干。我想当屏幕宽度(比如说iPhone5)变小时,用“尖叫”这个词,然后放到屏幕的顶部screen@IgorStrekha对不起,我不知道你想要什么。你能创建一个图像来展示你想要它的样子吗?我想让它像我的第一张照片一样。我想要宽度为“SimpleLabel”。并将边距添加到“下划线标签”。)但在代码背后不起作用。变量宽度=SimpleLabel.width;UnderlineLabel.Margin=新厚度(宽度,0,0,0)
<Grid Padding="0, 10, -10, 10" RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OpenHomePageCommand}"/>
</Grid.GestureRecognizers>
<controls:ExtendedLabel x:Name="SimpleLabel"
Grid.Row="0"
Grid.Column="0"
FontFamily="OpenSans-Semibold"
FontSize="16.4"
InputTransparent="True"
Text="I got it. Take me to the "
TextColor="{StaticResource Onahau}"
VerticalOptions="EndAndExpand" />
<controls:ExtendedLabel x:Name="UnderlineLabel"
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
FontFamily="OpenSans-Semibold"
FontSize="16.4"
InputTransparent="True"
IsUnderline="True"
Text="Home Screen"
TextColor="{StaticResource Onahau}"
VerticalOptions="EndAndExpand" />
</Grid>