Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6541991
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2018-06-06 10:34:17

将文本拟合为可用的大小
您可能需要将文本块合并到特定的矩形区域。 可以计算一个值
对于Label的FontSize属性,基于文本字符的数量,矩形区域的大小以及两个数字。
第一个数字是行间距。 这是每行文本的标签视图的垂直高度。 对于与三个平台关联的默认字体,它与FontSize属性大致相关,如下所示:
? iOS: lineSpacing = 1.2 * label.FontSize
? Android: lineSpacing = 1.2 * label.FontSize
? Windows Phone: lineSpacing = 1.3 * label.FontSize
第二个有用的数字是平均字符宽度。 对于默认字体的大写和小写字母的正常混合,平均字符宽度约为字体大小的一半,与平台无关:
? averageCharacterWidth = 0.5 * label.FontSize
例如,假设您想要包含宽度为320个单位的80个字符的文本字符串,并且希望字体大小尽可能大。 将宽度(320)除以字符数(40)的一半,即可得到8的字体大小,您可以将其设置为Label的FontSize属性。 对于有些不确定且不能事先测试的文本,您可能希望使此计算更保守一点以避免意外。
以下程序使用行间距和平均字符宽度来适合页面上的文本段落,减去状态栏占用的iPhone顶部区域。 为了在此程序中排除iOS状态栏,该程序使用ContentView。
ContentView派生自布局,但仅将Content属性添加到从布局继承的内容属性。 ContentView也是Frame的基类。 尽管ContentView除了占用矩形空间区域之外没有其他功能,但它有两个用途:大多数情况下,ContentView可以是其他视图的父级,以定义新的自定义视图。 但是ContentView也可以模拟边距。
正如您可能已经注意到的那样,Xamarin.Forms没有保证金的概念,传统上它与填充相似,不同之处在于填充位于视图和视图的一部分内,而保证位于视图之外,实际上是父视图的一部分。 ContentView让我们模拟这个。 如果您发现需要在视图上设置边距,请将该视图置于ContentView中,并将该Padding属性设置在ContentView上。 ContentView从Layout继承Padding属性。
EstimatedFontSize程序以稍微不同的方式使用ContentView:它在页面上设置惯用的填充以避免iOS状态栏,但随后将ContentView设置为该页面的内容。 因此,此ContentView与页面的大小相同,但不包括iOS状态栏。 正是在此ContentView上附加了SizeChanged事件,并且这是用于计算文本字体大小的此ContentView的大小。
SizeChanged处理程序使用第一个参数来获取触发事件的对象(本例中为ContentView),该对象是Label必须适合的对象。 计算在评论中描述:
 

点击(此处)折叠或打开

  1. public class EstimatedFontSizePage : ContentPage
  2. {
  3.     Label label;
  4.     public EstimatedFontSizePage()
  5.     {
  6.         label = new Label();
  7.         Padding = new Thickness(0,Device.OnPlatform(20, 0, 0), 0, 0);
  8.         ContentView contentView = new ContentView
  9.         {
  10.             Content = label
  11.         };
  12.         contentView.SizeChanged += OnContentViewSizeChanged;
  13.         Content = contentView;
  14.     }
  15.     void OnContentViewSizeChanged(object sender, EventArgs args)
  16.     {
  17.         string text =
  18.         "A default system font with a font size of S " +
  19.             "has a line height of about ({0:F1} * S) and an " +
  20.             "average character width of about ({1:F1} * S). " +
  21.             "On this page, which has a width of {2:F0} and a " +
  22.             "height of {3:F0}, a font size of ?1 should " +
  23.             "comfortably render the ??2 characters in this " +
  24.             "paragraph with ?3 lines and about ?4 characters " +
  25.             "per line. Does it work?";
  26.         // Get View whose size is changing.
  27.         View view = (View)sender;
  28.         // Define two values as multiples of font size.
  29.         double lineHeight = Device.OnPlatform(1.2, 1.2, 1.3);
  30.         double charWidth = 0.5;
  31.         // Format the text and get its character length.
  32.         text = String.Format(text, lineHeight, charWidth, view.Width, view.Height);
  33.         int charCount = text.Length;
  34.         // Because:
  35.         // lineCount = view.Height / (lineHeight * fontSize)
  36.         // charsPerLine = view.Width / (charWidth * fontSize)
  37.         // charCount = lineCount * charsPerLine
  38.         // Hence, solving for fontSize:
  39.         int fontSize = (int)Math.Sqrt(view.Width * view.Height /
  40.  (charCount * lineHeight * charWidth));
  41.         // Now these values can be calculated.
  42.         int lineCount = (int)(view.Height / (lineHeight * fontSize));
  43.         int charsPerLine = (int)(view.Width / (charWidth * fontSize));
  44.         // Replace the placeholders with the values.
  45.         text = text.Replace("?1", fontSize.ToString());
  46.         text = text.Replace("??2", charCount.ToString());
  47.         text = text.Replace("?3", lineCount.ToString());
  48.         text = text.Replace("?4", charsPerLine.ToString());
  49.         // Set the Label properties.
  50.         label.Text = text;
  51.         label.FontSize = fontSize;
  52.     }
  53. }


名为“?1”,“?? 2”,“?3”和“?4”的文本占位符被选择为唯一的,但其字符数与替换它们的数字相同。
如果目标是使文本尽可能大而文本不会溢出页面,则结果将验证该方法:

不错。 一点也不差。 文本实际上显示在所有三个平台上显示的少一行中,但该技术看起来很合理。 并非总是为横向模式计算相同的FontSize,但有时会发生这种情况:
阅读(2038) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~