问题
I have a button that I want some text in, but some text may be too long to fit nicely in the button. I would like to make the text scroll horizontally in one line like a marquee in HTML. I can get it to scroll in one line, however, the test text gets cut off at the edge of the button and the text that is there will actually move off of the button instead of disappearing at the edge of the button.
I have googled for an answer to my question and after a few hours, I think it is time to ask my question.
<Grid HeightRequest="400" Grid.Column="0" Grid.Row="0" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button BackgroundColor="#006633" Opacity="0.7" Grid.RowSpan="3" Grid.ColumnSpan="1">
</Button>
<Label x:Name="Label1" StyleClass="button" Grid.Row="1" Grid.Column="0" >
</Label>
</Grid>
public void Marque1()
{
Label1.Text = "This is to simulate a really long sentence for testing purposes";
Label1.HorizontalOptions = LayoutOptions.Start;
Label1.VerticalTextAlignment = TextAlignment.Center;
Label1.LineBreakMode = LineBreakMode.NoWrap;
Label1.TranslateTo(-50, 0, 8000, Easing.Linear);
}
I would like for the entire text to move from right to left and repeat, and not leave the boundaries of the button.
回答1:
you could check this,is it the effect you need:
public partial class MaqueText : ContentPage
{
private bool Execute { get; set; }
public MaqueText ()
{
InitializeComponent ();
Label1.Text = "This is to simulate a really long sentence for testing purposes";
Label1.HorizontalOptions = LayoutOptions.Start;
Label1.VerticalTextAlignment = TextAlignment.Center;
Label1.LineBreakMode = LineBreakMode.NoWrap;
}
protected override void OnAppearing()
{
base.OnAppearing();
Execute = true;
Device.StartTimer(TimeSpan.FromMilliseconds(50), () =>
{
Label1.TranslationX -= 5f;
if (Math.Abs(Label1.TranslationX) > Width)
{
Label1.TranslationX = Label1.Width;
}
return Execute;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Execute = false;
}
}
来源:https://stackoverflow.com/questions/57237615/how-to-make-label-text-scroll-automatically