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

相关推荐
2501_9445215921 分钟前
Flutter for OpenHarmony 微动漫App实战:标签筛选功能实现
android·开发语言·前端·javascript·flutter
kirk_wang39 分钟前
Flutter艺术探索-Hive高性能存储:NoSQL数据库实战
flutter·移动开发·flutter教程·移动开发教程
不会写代码0001 小时前
Flutter 框架跨平台鸿蒙开发 - 免费英语口语评测:AI智能发音纠正
人工智能·flutter·华为·harmonyos
Miguo94well1 小时前
Flutter框架跨平台鸿蒙开发——失物招领APP的开发流程
flutter·华为·harmonyos
小风呼呼吹儿1 小时前
Flutter 框架跨平台鸿蒙开发 - 全国公积金查询:智能公积金管理助手
flutter·华为·harmonyos
zilikew1 小时前
Flutter框架跨平台鸿蒙开发——小说人物生成APP开发流程
flutter·华为·harmonyos·鸿蒙
kirk_wang1 小时前
Flutter艺术探索-Flutter文件操作:path_provider与文件管理
flutter·移动开发·flutter教程·移动开发教程
@大迁世界1 小时前
Swift、Flutter 还是 React Native:2026 年你该学哪个
开发语言·flutter·react native·ios·swift
zilikew1 小时前
Flutter框架跨平台鸿蒙开发——图书馆座位预约APP开发流程
flutter·华为·harmonyos·鸿蒙
Miguo94well2 小时前
Flutter框架跨平台鸿蒙开发——每日天气APP的开发流程
flutter·华为·harmonyos