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

相关推荐
JMchen12311 小时前
跨技术栈:在Flutter/Compose中应用自定义View思想
java·经验分享·flutter·canvas·dart·自定义view
国医中兴14 小时前
Flutter 三方库 ngrouter 鸿蒙适配指南 - 实现高性能扁平化路由导航管理实战
flutter·harmonyos·鸿蒙·openharmony
lpftobetheone14 小时前
【Flutter】如何理解Dart语言的Isolate API
flutter
国医中兴15 小时前
Flutter 三方库 inject_generator 的鸿蒙化适配指南 - 自动化依赖注入注入生成器、驱动鸿蒙大型工程解耦实战
flutter·harmonyos·鸿蒙·openharmony·inject_generator
chdo15 小时前
从需求到实现:Flutter可变宽度滑动器的探索之路
flutter
国医中兴16 小时前
Flutter 三方库 themed_color_palette 的鸿蒙化适配指南 - 定义语义化调色板、在鸿蒙端实现像素级的主题切换实战
flutter·harmonyos·鸿蒙·openharmony·themed_color_palette
不爱吃糖的程序媛16 小时前
2026 年如何上车 Flutter-OH:环境搭建与上手流程
flutter
国医中兴16 小时前
Flutter 三方库 aws_sqs_api 鸿蒙适配指南 - 实现分布式消息异步解耦、在 OpenHarmony 上打造高可用云端队列控制中枢实战
flutter·harmonyos·aws
国医中兴17 小时前
Flutter 三方库 toggleable 鸿蒙适配指南 - 实现声明式状态切换逻辑、在 OpenHarmony 上打造极简交互组件引擎实战
flutter·交互·harmonyos
国医中兴17 小时前
Flutter 三方库 schedules 的鸿蒙化适配指南 - 玩转复杂时间调度算法、构建高效的鸿蒙办公协作系统实战
flutter·harmonyos·鸿蒙·openharmony