Flutter 的 TextBaseline 枚举中的字母和表意文字有什么区别
What is the difference between alphabetic and ideographic in Flutter's TextBaseline enum
Flutter中的TextBaseline
枚举有两种选择:
- 字母
- 表意文字
这些值实际上如何改变基线?
TextBaseline.alphabetic
字母基线是英文字母等字母所在的线。这是一个例子:
你可以看到英文字母很好地在线上,但它切断了汉字。
TextBaseline.ideographic
不过,当您使用表意文字选项时,基线位于文本区域的底部。请注意,汉字实际上并不正好在线上。相反,该行位于文本行的最底部。
补充代码
您可以将其插入 CustomPaint
小部件(如 here 所述)以重现上述示例。
@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);
}
Flutter中的TextBaseline
枚举有两种选择:
- 字母
- 表意文字
这些值实际上如何改变基线?
TextBaseline.alphabetic
字母基线是英文字母等字母所在的线。这是一个例子:
你可以看到英文字母很好地在线上,但它切断了汉字。
TextBaseline.ideographic
不过,当您使用表意文字选项时,基线位于文本区域的底部。请注意,汉字实际上并不正好在线上。相反,该行位于文本行的最底部。
补充代码
您可以将其插入 CustomPaint
小部件(如 here 所述)以重现上述示例。
@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);
}