当前位置: 首页 > 软件库 > 手机/移动开发 > >

auto_size_text

授权协议 MIT License
开发语言 Java
所属分类 手机/移动开发
软件类型 开源软件
地区 不详
投 递 者 乜华翰
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Flutter widget that automatically resizes text to fit perfectly within its bounds.

Show some ❤️ and star the repo to support the project

Resources:

Also check out the blazing fast key-value store hive.

Contents

Usage

AutoSizeText behaves exactly like a Text. The only difference is that it resizes text to fit within its bounds.

AutoSizeText(
  'The text to display',
  style: TextStyle(fontSize: 20),
  maxLines: 2,
)

Note: AutoSizeText needs bounded constraints to resize the text. More info here.

maxLines

The maxLines parameter works like you are used to with the Text widget. If there is no maxLines parameter specified, the AutoSizeText only fits the text according to the available width and height.

AutoSizeText(
  'A really long String',
  style: TextStyle(fontSize: 30),
  maxLines: 2,
)

Sample above

minFontSize & maxFontSize

The AutoSizeText starts with TextStyle.fontSize. It measures the resulting text and rescales it to fit within its bonds. You can however set the allowed range of the resulting font size.

With minFontSize you can specify the smallest possible font size. If the text still doesn't fit, it will be handled according to overflow. The default minFontSize is 12.

maxFontSize sets the largest possible font size. This is useful if the TextStyle inherits the font size and you want to constrain it.

AutoSizeText(
  'A really long String',
  style: TextStyle(fontSize: 30),
  minFontSize: 18,
  maxLines: 4,
  overflow: TextOverflow.ellipsis,
)

group

You can synchronize the font size of multiple AutoSizeText. They will fit their boundaries and all AutoSizeText in the same group have the same size. That means they adjust their font size to the group member with the smallest effective font size.

Note: If a AutoSizeText cannot adjust because of constraints like minFontSize, it won't have the same size as the other group members.

An instance of AutoSizeGroup represents one group. Pass this instance to all AutoSizeText you want to add to that group. You don't have to care about disposing the group if it is no longer needed.

Important: Please don't pass a new instance of AutoSizeGroup every build. In other words, save the AutoSizeGroup instance in a StatefulWidget.

var myGroup = AutoSizeGroup();

AutoSizeText(
  'Text 1',
  group: myGroup,
);

AutoSizeText(
  'Text 2',
  group: myGroup,
);

stepGranularity

The AutoSizeText will try each font size, starting with TextStyle.fontSize until the text fits within its bounds.
stepGranularity specifies how much the font size is decreased each step. Usually, this value should not be below 1 for best performance.

AutoSizeText(
  'A really long String',
  style: TextStyle(fontSize: 40),
  minFontSize: 10,
  stepGranularity: 10,
  maxLines: 4,
  overflow: TextOverflow.ellipsis,
)

presetFontSizes

If you want to allow only specific font sizes, you can set them with presetFontSizes.If presetFontSizes is set, minFontSize, maxFontSize and stepGranularity will be ignored.

AutoSizeText(
  'A really long String',
  presetFontSizes: [40, 20, 14],
  maxLines: 4,
)

overflowReplacement

If the text is overflowing and does not fit its bounds, this widget is displayed instead. This can be useful to prevent text being too small to read.

AutoSizeText(
  'A String tool long to display without extreme scaling or overflow.',
  maxLines: 1,
  overflowReplacement: Text('Sorry String too long'),
)

Rich Text

You can also use Rich Text (like different text styles or links) with AutoSizeText. Just use the AutoSizeText.rich() constructor(which works exactly like the Text.rich() constructor).

The only thing you have to be aware of is how the font size calculation works: The fontSize in the styleparameter of AutoSizeText (or the inherited fontSize if none is set) is used as reference.

For example:

AutoSizeText.rich(
  TextSpan(text: 'A really long String'),
  style: TextStyle(fontSize: 20),
  minFontSize: 5,
)

The text will be at least 1/4 of its original size (5 / 20 = 1/4).
But it does not mean that all TextSpans have at least font size 5.

Parameters

Parameter Description
key* Controls how one widget replaces another widget in the tree.
textKey Sets the key for the resulting Text widget
style* If non-null, the style to use for this text
minFontSize The minimum text size constraint to be used when auto-sizing text.
Is being ignored if presetFontSizes is set.
maxFontSize The maximum text size constraint to be used when auto-sizing text.
Is being ignored if presetFontSizes is set.
stepGranularity The step size in which the font size is being adapted to constraints.
presetFontSizes Predefines all the possible font sizes.
Important: presetFontSizes have to be in descending order.
group Synchronizes the size of multiple AutoSizeTexts
textAlign* How the text should be aligned horizontally.
textDirection* The directionality of the text. This decides how textAlign values like TextAlign.start and TextAlign.end are interpreted.
locale* Used to select a font when the same Unicode character can be rendered differently, depending on the locale.
softWrap* Whether the text should break at soft line breaks.
wrapWords Whether words which don't fit in one line should be wrapped. Defaults to true to behave like Text.
overflow* How visual overflow should be handled.
overflowReplacement If the text is overflowing and does not fit its bounds, this widget is displayed instead.
textScaleFactor* The number of font pixels for each logical pixel. Also affects minFontSize, maxFontSize and presetFontSizes.
maxLines An optional maximum number of lines for the text to span.
semanticsLabel* An alternative semantics label for this text.

Parameters marked with * behave exactly the same as in Text

Performance

AutoSizeText is really fast. In fact, you can replace all your Text widgets with AutoSizeText.
Nevertheless you should not use an unreasonable high fontSize in your TextStyle. E.g. don't set the fontSize to 1000 if you know, that the text will never be larger than 30.

If your font size has a very large range, consider increasing stepGranularity.

Troubleshooting

Missing bounds

If AutoSizeText overflows or does not resize the text, you should check if it has constrained width and height.

Wrong code:

Row(
  children: <Widget>[
    AutoSizeText(
      'Here is a very long text, which should be resized',
      maxLines: 1,
    ),
  ],
)

Because Row and other widgets like Container, Column or ListView do not constrain their children, the text will overflow.
You can fix this by constraining the AutoSizeText. Wrap it with Expanded in case of Row and Column or use a SizedBox or another widget with fixed width (and height).

Correct code:

Row(
  children: <Widget>[
    Expanded( // Constrains AutoSizeText to the width of the Row
      child: AutoSizeText(
        'Here is a very long text, which should be resized',
        maxLines: 1,
      )
    ),
  ],
)
}

Further explanation can be found here. If you still have problems, please open an issue.

MinFontSize too large

AutoSizeText does not resize text below the minFontSize which defaults to 12. This can happen very easily if you use AutoSizeText.rich():

Wrong code:

AutoSizeText.rich(
  TextSpan(
    text: 'Text that will not be resized correctly in some cases',
    style: TextStyle(fontSize: 200),
  ),
)

This rich text does not have a style so it will fall back to the default style (probably fontSize = 14). It has an implicit minFontSize of 12 that means it can be resized to 86% of its original size at the most (14 -> 12). Just set minFontSize = 0 or add style: TextStyle(fontSize: 200) to the AutoSizeText.

Note: If you use the first option, you should also consider lowering stepGranularity. Otherwise the steps of resizing will be very large.

Correct code:

AutoSizeText.rich(
  TextSpan(
    text: 'Text that will be resized correctly',
    style: TextStyle(fontSize: 200),
  ),
  minFontSize: 0,
  stepGranularity: 0.1,
)

MIT License

Copyright (c) 2018 Simon Leier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • 场景 因为许多网站还没有适配屏幕较小的设备,移动设备的浏览器在渲染网页时可能会有不同。他们不是以设备屏幕宽度布局网页,而是用比屏幕宽一些的 视窗 去布局网页,通常是 800 到 1000 像素。为了将视窗上的布局映射到原始设备屏幕上,手机浏览器要么只渲染整个页面的一部分,要么将视窗缩放至原始屏幕大小。 因为缩放适配小屏幕而导致文字会变得很小,许多手机浏览器会使用文本溢出算法放大文本,改善可读性。当

  • text-size-adjust 的用法 text-size-adjust会根据设备尺寸而自动调整文字大小,这对于前端效果并不一定是需要的,所以我们一般会修改这个属性,将字体大写严格设置为我们定义的大小 html { -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; -moz-text-size-adjust: 100%; te

  • public class AutofitTextView extends TextView implements  AutofitHelper.OnTextSizeChangeListener {     private AutofitHelper mHelper;     public AutofitTextView(Context context) {         super(contex

  • size_t 类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.h的C++版。它是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。 例如:在用下标访问元素时,vector使用vector::size_type作为下标类型,而数组下标的正确类型则是size_t。vector使用的下标实际也是size_t,源码是typedef size_t size_t

  • 1、当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 html{-webkit-text-size-adjust:none;} 2、-webkit-text-size-adjust放在body上会导致页面缩放失效 3、body会继承定义在html的样式 4、用-webkit-text-size-adjust不要定义成可继承的或全局的 苹果移动设备

  • iPhone 和 Android 的浏览器纵向 (Portrate mode) 和橫向 (Landscape mode) 模式皆有自动调整字体大小的功能。控制它的就是 CSS 中的 -webkit-text-size-adjust。 ext-size-adjust 设为 none 或者 100% 关闭字体大小自动调整功能.

  • autosize.js是一个自动调整大小是一个小的脚本,可以自动调整文本区域的高度以适合文本内容。 我们很多地方都能用的到,比如textarea 里面有初始化内容,高度默认100px,当鼠标点击进去的时候,文本区域高度自适应内容高度显示,方便编辑内容。 官方网址:Autosize 使用方法: autosize 函数接受单个 textarea 元素,或者 textarea 元素的数组或类似数组的对象

  • <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="style.css" /> <

  • -webkit-text-size-adjust的用法如下: 1、之前可以设置一个百分比然后可以在网页上(pc)显示小于12px的字体,但后来谷歌新版本已经不支持这个属性了。如果还想实现小于12px的字体,要用-webkit-transform:scale(0.8) 2、在移动设备上如手机和平板横屏会导致字体变大,-webkit-text-size-adjust: 100%可以禁止字体变化。 3、

  • 最新版的Chrome浏览器已经不再支持-webkit-text-size-adjust的使用 -webkit-text-size-adjust:100%;设置设备可调节文字大小,可随网页的缩放放大缩小,特别是iphon竖屏转横屏时,可以让文字自动适应调整后的屏幕。 。-webkit-text-size-adjust:none;设置设备不可调节文字大小,即不随网页的的缩放放大缩小。 最后在制作网页时

  • 写入 text="这里是写入内容123" files.write("/sdcard/1.txt",text) 追加 //追加内容 \n换行 files.append("/sdcard/1.txt","\n1234567899") 读取文本 //读取文本 if(files.isFile("/sdcard/1.txt")){ var txt = files.read("/sdcar

  • val textStyleBody1 = TextStyle( color = HaiveColor_Main, fontSize = 18.sp, fontWeight = FontWeight.SemiB

  • 前提:使用前需要加上兼容写法 用法: 在移动端为防止字体变大,添加-text-size-adjust属性为100%可防止字体变大 放在body会导致页面缩放失效 可以使body继承html的样式