【发布时间】:2023-01-08 12:48:31
【问题描述】:
对于这个例子:
var vm.MyText = "ABC";
<Label Grid.Row="1" Text="{Binding MyText}" />
有没有办法可以在文本中添加下划线?
【问题讨论】:
标签: xamarin xamarin.forms
对于这个例子:
var vm.MyText = "ABC";
<Label Grid.Row="1" Text="{Binding MyText}" />
有没有办法可以在文本中添加下划线?
【问题讨论】:
标签: xamarin xamarin.forms
使用TextDecorations="Underline"(需要 3.3.0)
<Label>
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="This app is written in C#, XAML, and native APIs using the" />
<Span Text=" " />
<Span Text="Xamarin Platform" FontAttributes="Bold" TextColor="Blue" TextDecorations="Underline">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://learn.microsoft.com/en-us/xamarin/xamarin-forms/"/>
</Span.GestureRecognizers>
</Span>
<Span Text="." />
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
【讨论】:
您可以使用 FormattedString 将不同的属性、颜色等应用于标签:
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "Stack, ", FontAttributes = FontAttributes.None });
formattedString.Spans.Add(new Span { Text = "Overflow, ", FontAttributes = FontAttributes.Italic });
label.FormattedText = formattedString;
回复:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text
【讨论】:
编辑:如果您运行的是 3.3.0 之前的 XF 版本,请检查此答案,否则请遵循已接受的答案。
如果你需要的是下划线,你必须创建一个效果
使用 Xamarin.Forms;
namespace YourProjectNamespace.Effects
{
public class UnderlineTextEffect : RoutingEffect
{
public UnderlineTextEffect()
: base("YourProjectNamespace.UnderlineTextEffect")
{
}
}
}
安卓实现
using System;
using System.ComponentModel;
using Android.Graphics;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ResolutionGroupName("YourProjectNamespace")]
[assembly: ExportEffect(typeof(AndroidUnderlineTextEffect), "UnderlineTextEffect")]
namespace YourProjectNamespace.Android.Effects
{
public class AndroidUnderlineTextEffect : PlatformEffect
{
protected override void OnAttached()
{
((TextView)Control).PaintFlags |= PaintFlags.UnderlineText;
}
protected override void OnDetached()
{
}
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);
if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
((TextView)Control).PaintFlags |= PaintFlags.UnderlineText;
}
}
}
iOS 实现:
using System;
using System.ComponentModel;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ResolutionGroupName("YourProjectNamespace")]
[assembly: ExportEffect(typeof(AppleUnderlineTextEffect), "UnderlineTextEffect")]
namespace YourProjectNamespace.iOS.Effects
{
public class AppleUnderlineTextEffect : PlatformEffect
{
protected override void OnAttached()
{
SetTextUnderline();
}
protected override void OnDetached()
{
}
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);
if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
SetTextUnderline();
}
private void SetTextUnderline()
{
var text = ((UILabel)Control).AttributedText as NSMutableAttributedString;
var range = new NSRange(0, text.Length);
text.AddAttribute(UIStringAttributeKey.UnderlineStyle,
NSNumber.FromInt32((int)NSUnderlineStyle.Single),
range);
}
}
}
在您的 XAML 上添加效果:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YourProjectNamespace"
x:Class="YourProjectNamespace.UnderlineEffectPage">
<StackLayout>
<Label
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"
Text="Underlined Text">
<Label.Effects>
<local:UnderlineTextEffect />
</Label.Effects>
</Label>
</StackLayout>
</ContentPage>
【讨论】: