Flutter的文字高度及行高简单计算


行高度简单示意图

以RichText为例,可以通过设置style以及strutStyle共同作用文字的样式,

一、其中当仅设置style时:

  1. 当height不为空

字体及行高 = fontSize*height

  1. 当height为空

字体及行高 取决于 字体的量度及其字体大小

如下为官网对height和行高关系的解释

复制代码
 /// When [height] is null or omitted, the line height will be determined
/// by the font's metrics directly, which may differ from the fontSize.
/// When [height] is non-null, the line height of the span of text will be a
/// multiple of [fontSize] and be exactly `fontSize * height` logical pixels
/// tall.

如下为字体规格和行高的关系示意图

如果此时设置字体的背景 则背景的高度和字体的高度及行高都一致

一、当仅设置strutStyle时:

flutter的struct概念可参考en.wikipedia.org 及 CSS的行高概念Visual formatting model details

它的作用可以简单的理解为在每一行开始的地方插入一个没有宽度 和strutStyle定义的高度相同的字符。如果text中的字符高度小于strutStyle的高度则以strutStyle高度为最终值,如果大于strutStyle的高度,则不受影响正常布局。strutStyle定义了行的最小高度,

但是若strutStyle定义forceStrutHeight为true,则所有的字符样式都使用strutStyle规定的高度

strutStyle的高度

计算公式为:

fontSize * height + fontSize * leading

参考:

https://api.flutter.dev/flutter/painting/TextStyle-class.html

https://api.flutter.dev/flutter/painting/StrutStyle-class.html

https://en.wikipedia.org/wiki/Strut_(typesetting)

https://www.w3.org/TR/CSS2/visudet.html#line-height

相关推荐
瓜子三百克4 小时前
七、性能优化
flutter·性能优化
恋猫de小郭11 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
小蜜蜂嗡嗡18 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
瓜子三百克1 天前
十、高级概念
flutter
帅次1 天前
Objective-C面向对象编程:类、对象、方法详解(保姆级教程)
flutter·macos·ios·objective-c·iphone·swift·safari
小蜜蜂嗡嗡1 天前
flutter flutter_vlc_player播放视频设置循环播放失效、初始化后获取不到视频宽高
flutter
孤鸿玉2 天前
[Flutter小技巧] Row中widget高度自适应的几种方法
flutter
bawomingtian1232 天前
FlutterView 源码解析
flutter
Zender Han2 天前
Flutter 进阶:实现带圆角的 CircularProgressIndicator
flutter