如何让标签文本自动滚动? [英] How to make Label text scroll automatically?

查看:35
本文介绍了如何让标签文本自动滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,我想在其中输入一些文本,但有些文本可能太长而无法很好地放入按钮中.我想让文本像 HTML 中的选取框一样在一行中水平滚动.我可以让它滚动一行,但是,测试文本在按钮的边缘被截断,那里的文本实际上会从按钮上移开,而不是在按钮的边缘消失.

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.

推荐答案

你可以看看这个,是你需要的效果:

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;
    }
}

这篇关于如何让标签文本自动滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆