【问题标题】:Forms9Patch - Xamarin label auto sizing - bounds autofit and initial font sizeForms9Patch - Xamarin 标签自动调整大小 - 边界自动调整和初始字体大小
【发布时间】:2019-11-13 12:20:59
【问题描述】:

在 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 是否有一种内置方式来处理屏幕缩放?

【问题讨论】:

  • @baskren 有什么线索吗?顺便说一句,吉米你试过 Lines = 0 吗?
  • @Saamer 我试过了,但我刚才又试了一次。这启用了垂直动态调整大小。所以,我上面的代码水平调整大小,并使用 Lines=0 垂直调整大小。现在,如果我能让两者一起工作......
  • 因此将其更改为 0 会禁用水平调整大小?
  • @Saamer 是的。水平缩小窗口时字体会被截断。它永远不会改变大小。
  • 我知道了。是的,我就是这样做的,结果是一样的。它可以水平或垂直工作。但是,我刚刚在下面看到你的答案......

标签: c# xaml xamarin xamarin.forms uwp


【解决方案1】:

或者,您可以尝试使用类似的方法,在 ContentView 中添加私有字段 label,然后向其中添加 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

【讨论】:

  • 这似乎有效。这是一个很好的解决方案。我从来没有看过那个文档,所以谢谢你。我试过了,它处理屏幕缩放,缩小和扩大视图。我还在玩它,但它看起来会满足我的需求。
猜你喜欢
  • 2011-02-07
  • 1970-01-01
  • 2011-02-28
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
相关资源
最近更新 更多