calculateTextHeight static method

double calculateTextHeight(
  1. BuildContext context,
  2. String? value,
  3. double fontSize,
  4. int maxLines,
)

Implementation

static double calculateTextHeight(
    BuildContext context, String? value, double fontSize, int maxLines) {
  TextPainter painter = TextPainter(

      // AUTO:华为手机如果不指定locale的时候,该方法算出来的文字高度是比系统计算偏小的。
      locale: Localizations.localeOf(context),
      maxLines: maxLines,
      textAlign: TextAlign.end,
      textDirection: TextDirection.ltr,
      strutStyle: _contentStructStyle,
      text: TextSpan(
          text: value,
          style: TextStyle(
            height: 1,
            fontSize: fontSize,
          )));
  painter.layout();

  // 文字的高度:painter.height
  return painter.height < fontSize ? fontSize : painter.height;
}