Forms9Patch-Xamarin标签自动调整大小-边界自动调整和初始字体大小

时间:2019-07-02 20:13:25

标签: c# xaml xamarin xamarin.forms uwp

在Xamarin Forms中使用Forms9Patch在UWP上运行并水平更改窗口大小时,我能够使字体动态调整大小。大小调整得很完美。

但是,我有一些我无法解决的问题...

  1. 当我垂直缩小窗口时,文本不会调整大小。我在做

    Lines = 1;
    AutoFit = Forms9Patch.AutoFit.Width;
    LineBreakMode = LineBreakMode.NoWrap;
    

我回到这里-https://baskren.github.io/Forms9Patch/guides/Label.html,然后重新阅读。它说施加边界自动拟合以实现此目的。我已经尝试过并且无法正常工作。使它正常工作的正确语法是什么?

  1. 标签的起始字体大小。我现在正在硬编码。有没有一种方法可以在启动时动态调整大小?

  2. 我有一个解决方法,但是Forms9Patch是否有一种内置的方式来处理屏幕缩放?

1 个答案:

答案 0 :(得分:1)

或者,您可以尝试使用类似的方法,在label内添加私有字段ContentView,然后向其添加SizeChanged事件

ContentView contentView = new ContentView
{
          Content = label
};
contentView.SizeChanged += OnContentViewSizeChanged;

并在事件中

void OnContentViewSizeChanged(object sender, EventArgs args)
{
         string text = "Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams. Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.";
         View view = (View)sender;

         // Line height is 1.2 if it's iOS or Android, but 1.3 for UWP
         double lineHeight = 1.3;
         double charWidth = 0.5;
         text = String.Format(text, lineHeight, charWidth, view.Width, view.Height);
         int charCount = text.Length;
         int fontSize = (int)Math.Sqrt(view.Width * view.Height / (charCount * lineHeight * charWidth));
         int lineCount = (int)(view.Height / (lineHeight * fontSize));
         int charsPerLine = (int)(view.Width / (charWidth * fontSize));
         label.Text = text;
         label.FontSize = fontSize;
}

这里有一些official documentation推荐