我正在使用net maui标签控件。我想只旋转标签内的字符串字符而不旋转标签。我如何才能实现这一点?我想实现旋转标签内的文字,而不旋转标签?
swvgeqrz1#
您可以创建一个自定义控件来实现您要做的事情。就像这样。第一个月
<?xml version="1.0" encoding="utf-8"?><ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.RotatableLabel"> <HorizontalStackLayout Spacing="5" x:Name="LabelContainer" /></ContentView>
<?xml version="1.0" encoding="utf-8"?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.RotatableLabel">
<HorizontalStackLayout
Spacing="5"
x:Name="LabelContainer" />
</ContentView>
字符串RotatableLabel.xaml.cs的
RotatableLabel.xaml.cs
public partial class RotatableLabel : ContentView{ public RotatableLabel() { InitializeComponent(); } private string? _text; public string? Text { get => _text; set { _text = value; HandleRotation(); } } private int _textRotation; public int TextRotation { get => _textRotation; set { _textRotation = value; HandleRotation(); } } private void HandleRotation() { if (Text == null) return; LabelContainer.Clear(); foreach (var c in Text) { LabelContainer.Add(new Label { Text = $"{c}", Rotation = TextRotation }); } }}
public partial class RotatableLabel : ContentView
{
public RotatableLabel()
InitializeComponent();
}
private string? _text;
public string? Text
get => _text;
set
_text = value;
HandleRotation();
private int _textRotation;
public int TextRotation
get => _textRotation;
_textRotation = value;
private void HandleRotation()
if (Text == null)
return;
LabelContainer.Clear();
foreach (var c in Text)
LabelContainer.Add(new Label
Text = $"{c}",
Rotation = TextRotation
});
型你会像这样使用它
<test:RotatableLabel Text="Hello World" TextRotation="90" />
<test:RotatableLabel
Text="Hello World"
TextRotation="90" />
型它当然可以增强,例如使用BindableProperties,更好地计算字母之间的间距和处理Label属性,但这是一个很好的起点。
Label
1条答案
按热度按时间swvgeqrz1#
您可以创建一个自定义控件来实现您要做的事情。
就像这样。
第一个月
字符串
RotatableLabel.xaml.cs
的型
你会像这样使用它
型
它当然可以增强,例如使用BindableProperties,更好地计算字母之间的间距和处理
Label
属性,但这是一个很好的起点。