.net 标签内字符串的旋转

uqjltbpv  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(186)

我正在使用net maui标签控件。我想只旋转标签内的字符串字符而不旋转标签。我如何才能实现这一点?
我想实现旋转标签内的文字,而不旋转标签?

swvgeqrz

swvgeqrz1#

您可以创建一个自定义控件来实现您要做的事情。
就像这样。
第一个月

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="MyApp.RotatableLabel">
  5. <HorizontalStackLayout
  6. Spacing="5"
  7. x:Name="LabelContainer" />
  8. </ContentView>

字符串
RotatableLabel.xaml.cs

  1. public partial class RotatableLabel : ContentView
  2. {
  3. public RotatableLabel()
  4. {
  5. InitializeComponent();
  6. }
  7. private string? _text;
  8. public string? Text
  9. {
  10. get => _text;
  11. set
  12. {
  13. _text = value;
  14. HandleRotation();
  15. }
  16. }
  17. private int _textRotation;
  18. public int TextRotation
  19. {
  20. get => _textRotation;
  21. set
  22. {
  23. _textRotation = value;
  24. HandleRotation();
  25. }
  26. }
  27. private void HandleRotation()
  28. {
  29. if (Text == null)
  30. return;
  31. LabelContainer.Clear();
  32. foreach (var c in Text)
  33. {
  34. LabelContainer.Add(new Label
  35. {
  36. Text = $"{c}",
  37. Rotation = TextRotation
  38. });
  39. }
  40. }
  41. }


你会像这样使用它

  1. <test:RotatableLabel
  2. Text="Hello World"
  3. TextRotation="90" />


它当然可以增强,例如使用BindableProperties,更好地计算字母之间的间距和处理Label属性,但这是一个很好的起点。

展开查看全部

相关问题