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

相关推荐
你听得到112 小时前
排查 App 问题:别只盯着报错,把前后发生的事情串起来
android·前端·flutter
天空之城--3 小时前
近一周Android Flutter行业动态与实用参考
android·flutter
GitLqr17 小时前
玩转 Flutter 项目依赖:从 pubspec.yaml 到实战包管理
flutter·面试·全栈
程序员老刘1 天前
Qwen 3.8 max干了20分钟没干完,免费模型3分17秒搞定,问题出在哪?
flutter·ai编程
飞凌嵌入式1 天前
告别多端重复开发:Buildroot+Flutter实现全平台UI风格统一
flutter·ui
FungLeo1 天前
Flutter 带 TTL 的多级缓存设计:内存+磁盘+网络三层实战
网络·flutter·缓存·性能优化
FungLeo1 天前
Flutter 可复用公共组件库设计与落地:AppDialog/BottomSheet 等实战
flutter
Fungleo1069972 天前
Flutter/Android Release 包连不上网?AndroidManifest INTERNET 权限排查实录
flutter
GitLqr2 天前
Flutter 实战:为你的 App 增加桌面快捷方式 (Quick Actions)
flutter·app·全栈
FungLeo2 天前
Flutter 把第三方 UI 库渐进迁回 Material 3:组件映射总表 + 四批次替换实战
flutter·ui