简体   繁体   English

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

[英]What is the difference between alphabetic and ideographic in Flutter's TextBaseline enum

The TextBaseline enum in Flutter has two options: Flutter 中的TextBaseline枚举有两个选项:

  • alphabetic字母
  • ideographic表意的

How do these values actually change the baseline?这些值实际上如何改变基线?

TextBaseline.alphabetic TextBaseline.alphabetic

The alphabetic baseline is the line that the letters in alphabets like English sit on.字母基线是字母表中的字母(如英语)所在的线。 Here is an example:下面是一个例子:

在此处输入图片说明

You can see that the English letters sit nicely on the line, but it cuts through the Chinese characters.你可以看到英文字母很好地排在一行上,但它贯穿了汉字。

TextBaseline.ideographic TextBaseline.ideographic

When you use the ideographic option, though, the baseline is at the bottom of the text area.但是,当您使用表意选项时,基线位于文本区域的底部。 Note that the Chinese characters don't actually sit right on the line.请注意,汉字实际上并没有直接放在行上。 Rather, the line is at the very bottom of the text line.相反,该行位于文本行的最底部。

在此处输入图片说明

Supplemental code补充代码

You can plug this into a CustomPaint widget (as described here ) to reproduce the above examples.你可以插入该成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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM