Geo*_*old 19 java text line-breaks word-wrap
在我重新发明轮子(很差)之前,我想知道是否有一些现有的Java代码用于将文本行包装到给定的最大宽度.理想情况下它会:
编辑:这里没有"像素",只有java.lang.String."最大宽度"是指一行中的字符数.
Rom*_*nko 25
Apache commons中有WordUtils和wrap函数:
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html
PS看起来这是被摧毁的,你需要使用
代替.
使用自动换行库(可在 Maven Central 上找到)。
这是一种使用方法:
String text = "hi there how are you going?";
String wrapped =
WordWrap.from(text)
.maxWidth(10)
.insertHyphens(true) // true is the default
.wrap();
Run Code Online (Sandbox Code Playgroud)
输出是:
hi there
how are
you going?
Run Code Online (Sandbox Code Playgroud)
该库保留了行前导空格,这是对 Apache commons-lang产品行为的抱怨之一。您还可以指定stringWidth函数以在渲染文本时获得像素精确的结果。
该库具有不错的单元测试覆盖率(当您考虑从网络复制和粘贴代码块时要记住这一点!)。
Maven 依赖项是:
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>word-wrap</artifactId>
<version>0.1.9</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
请务必检查是否有更高版本。
| 归档时间: |
|
| 查看次数: |
28445 次 |
| 最近记录: |