[英]Text aligned with link button in Xamarin Forms
I'm working on an app where several times there are text labels combined with buttons or where a part of the text has a different color. 我正在开发一个应用程序,其中有几次文本标签与按钮组合或文本的一部分具有不同的颜色。
This time, I need to achieve this: 这一次,我需要实现这个目标:
The "Sign in" text button has to have an action attached to it and also have a different look from the rest of the text. “登录”文本按钮必须附加一个操作,并且与文本的其余部分具有不同的外观。
I have tried with a StackView with the Horizontal attribute but it just depends too much on the width of the device and text length and it doesn't look centered horizontally on the screen. 我尝试使用具有Horizontal属性的StackView,但它仅仅取决于设备的宽度和文本长度,并且它不会在屏幕上水平居中。
On Html I would do something like 在Html上我会做类似的事情
Already have an account? <a>Sign in</a>
Is there something like that on XAML? 在XAML上有类似的东西吗?
Yes - spans. 是的 - 跨度。 You can build a formatted string using spans that have different fonts, colours, sizes etc.
您可以使用具有不同字体,颜色,大小等的跨度来构建格式化字符串。
<Label>
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Hello" ForegroundColor="Red" FontAttributes="Italic" FontSize="10" />
<Span Text="World" ForegroundColor="Blue" FontSize="10" />
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
Span text is not bindable, but FormattedText on Label is, so you can build a FormattedString in your view model and bind that to the FormattedText property. Span文本不可绑定,但Label上的FormattedText是,因此您可以在视图模型中构建FormattedString并将其绑定到FormattedText属性。
A grid with auto-sized columns and a TapGestureRecognizer
on the Sign In
label is one approach that will produce a consistent look on iOS and Android as buttons are not used. Sign In
标签上有自动大小列和TapGestureRecognizer
的网格是一种在iOS和Android上产生一致外观的方法,因为不使用按钮。
<Grid Padding="20">
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="Already have an account?" TextColor="Black" BackgroundColor="White" />
<Label x:Name="onSignIn" Grid.Row="0" Grid.Column="1" Text="Sign In" TextColor="Red" BackgroundColor="White" />
</Grid>
In the code behind: 在后面的代码中:
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (sender, e) => D.WriteLine("Tapped");
onSignIn.GestureRecognizers.Add(tapGestureRecognizer);
您也可以尝试这个现有的解决方案: https : //theconfuzedsourcecode.wordpress.com/tag/xamarin-hyperlink-label/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.