我想控制pdf文件中文本的宽度不溢出怎么办?

问题描述 投票:0回答:1
pw.Text(descDiv, textAlign: pw.TextAlign.left,  style:const pw.TextStyle(fontSize: 10)),

但是文本溢出不起作用

pw.Text(descDiv, textAlign: pw.TextAlign.left,  style:const pw.TextStyle(fontSize: 10), overflow: pw.TextOverflow.ellipsis,),

有人帮助我吗??

flutter text width
1个回答
0
投票

你可以试试这个功能

String handleOverflow(String text, double maxWidth) {
  const ellipsis = '...';

  if (text.length <= 3 || maxWidth <= 0) {
    return text;
  }

  final ellipsisWidth = ellipsis.characters.length * 5; 

  while (text.length > 1 && text.length * 5 > maxWidth - ellipsisWidth) {
    text = text.substring(0, text.length - 1);
  }

  return text + ellipsis;
}

然后像这样使用它

pw.Text(
    handleOverflow(descDiv, 200), 
    textAlign: pw.TextAlign.left,
    style: pw.TextStyle(fontSize: 10),
  )
© www.soinside.com 2019 - 2024. All rights reserved.