Flutter 的 TextBaseline 枚举中的字母和表意有什么区别

Sur*_*gch 3 text baseline fontmetrics flutter

TextBaselineFlutter 中的enum 有两个选项:

  • 字母
  • 表意的

这些值实际上如何改变基线?

Sur*_*gch 10

TextBaseline.alphabetic

字母基线是字母表中的字母(如英语)所在的线。下面是一个例子:

在此处输入图片说明

你可以看到英文字母很好地排在一行上,但它贯穿了汉字。

TextBaseline.ideographic

但是,当您使用表意选项时,基线位于文本区域的底部。请注意,汉字实际上并没有直接放在行上。相反,该行位于文本行的最底部。

在此处输入图片说明

补充代码

你可以插入该成CustomPaint微件(如所描述的在这里)来再现上述的例子。

@override
void paint(Canvas canvas, Size size) {
  final textStyle = TextStyle(
    color: Colors.black,
    fontSize: 30,
  );
  final textSpan = TextSpan(
    text: 'My text ??',
    style: textStyle,
  );
  final textPainter = TextPainter(
    text: textSpan,
    textDirection: TextDirection.ltr,
  );
  textPainter.layout(
    minWidth: 0,
    maxWidth: size.width,
  );

  print('width: ${textPainter.width}');
  print('height: ${textPainter.height}');

  // draw a rectangle around the text
  final left = 0.0;
  final top = 0.0;
  final right = textPainter.width;
  final bottom = textPainter.height;
  final rect = Rect.fromLTRB(left, top, right, bottom);
  final paint = Paint()
    ..color = Colors.red
    ..style = PaintingStyle.stroke
    ..strokeWidth = 1;
  canvas.drawRect(rect, paint);

  // draw the baseline
  final distanceToBaseline =
      textPainter.computeDistanceToActualBaseline(TextBaseline.ideographic);
  print('distanceToBaseline: ${distanceToBaseline}');
  canvas.drawLine(
    Offset(0, distanceToBaseline),
    Offset(textPainter.width, distanceToBaseline),
    paint,
  );

  // draw the text
  final offset = Offset(0, 0);
  textPainter.paint(canvas, offset);
}
Run Code Online (Sandbox Code Playgroud)