import 'package:flutter/material.dart';

class TextUtil { /// 获取文字高度 static double calculateTextHeight(String value, double fontSize, { required double fontHeight, required double maxWidth, required EdgeInsetsGeometry padding }) { TextPainter painter = TextPainter( locale: WidgetsBinding.instance!.platformDispatcher.locale, textDirection: TextDirection.ltr, maxLines: 1000, strutStyle: StrutStyle( forceStrutHeight: true, fontSize: fontSize, height: fontHeight ), text: TextSpan( text: value, style: TextStyle( height: fontHeight, fontSize: fontSize, ), ), textAlign: TextAlign.center ); painter.layout(maxWidth: maxWidth - padding.horizontal); return painter.size.height; } }

Flutter 计算文字高度:TextUtil 类详解

原文地址: https://www.cveoy.top/t/topic/bRwV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录