在 LineBreakMeasurer 中处理 \n
有数千篇文章如何使用 LineBreakMeasurer 绘制多行文本,但没有一篇关于绘制多行文本并考虑到 \n(当您想在文本中的特定位置强制换行时,而不仅仅是当右边距或左边距结束)。
秘密似乎在于 BreakIterator,但我找不到处理 \n 的实现。
There are thousand articles how to use LineBreakMeasurer to draw multi-line text but there is none about drawing multi-line text taking into account also \n(when you want to force a new line at a specific position in text and not only when the right - or left - margin ends).
The secret seems to lie in BreakIterator, but I couldn't find an implementation which handles \n.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用重载的
LBM.nextLayout(float, int) 代替
方法。 这允许您限制LineBreakMeasurer
的 (LBM
)nextLayout(float)
方法, 布尔值)LBM
将包含在返回的TextLayout
中的文本。 就您而言,您将指示它不要超出下一个换行符。这段代码应该能让你明白。 首先使用 LBM.nextOffset 来“查看”哪个字符索引将是下一个布局的末尾。 然后迭代字符串内容直到该偏移量,看看是否找到任何换行符。 这将告诉
LBM
不要超过换行符如果这样做,则使用找到的限制作为
nextLayout(float, int, boolean)
的第二个参数,: ="http://java.sun.com/developer/onlineTraining/Media/2DText/style.html#layout" rel="nofollow">http://java.sun.com/developer/onlineTraining/Media/2DText/ style.html#layout
http://java.sun.com/developer/onlineTraining/Media/2DText/Code/LineBreakSample.java
Instead of
LineBreakMeasurer
's (LBM
's)nextLayout(float)
method, use the overloadedLBM.nextLayout(float, int, boolean)
method. This allows you to limit the text thatLBM
will include in the returnedTextLayout
. In your case, you'll instruct it not to go beyond the next newline.This code snippet should give you the idea. First use
LBM.nextOffset
to "peek" which character index would be the end of the next layout. Then iterate over your string content up to that offset to see if you find any newline characters. If you do, then use that found limit as the second argument tonextLayout(float, int, boolean)
which will tellLBM
not to exceed the newline:References
http://java.sun.com/developer/onlineTraining/Media/2DText/style.html#layout
http://java.sun.com/developer/onlineTraining/Media/2DText/Code/LineBreakSample.java
我发现这段代码对于换行问题很有效。 我使用 atdixon 作为模板来得到这个。
I find that this code works well for the newline issue. I used atdixon as a template to get this.
首先对文本进行标记,然后将 LineBreakMeasureCode 应用于每个标记。
Tokenize the text first, then just apply the LineBreakMeasureCode to each token.
Aaron 的代码并不总是正确工作,因此这里有一些对我有用的调整后的代码:
如果您需要来自 AttributedString 的文本,您可以预先执行此操作
Aaron's code doesn't always work right so here's some tweaked code that is working for me:
If you need text from an AttributedString you can just do this beforehand
尽管这个话题很老了,但我自己也遇到过这个问题并且必须解决它。 经过大量的调查后,我提出了可以在包装“JTextArea”的单个类中工作的解决方案。
代码是 Kotlin 语言,因为我正在使用它。 希望它仍然有用。
另外,一个可以让您测试行计数器的 GUI 应用程序也可能很有用。
更新
经过进一步调查,我注意到计算器仍然有问题,需要一些定制。 因此,我改进了计算机制,以提供内部组成的文本中断的详细信息。
这种机制大部分时间都有效。 我注意到有几种情况,其中
JTextArea
会用空行换行,但未检测到。 因此,使用该代码的风险由您自行承担。Even though the topic is very old, I’ve had this issue myself and had to solve it. After considerable amount of investigation, I’ve come up with solution that would work in single class that wraps ’JTextArea’.
The code is in Kotlin, as that is what I’m using. Hopefully it’ll still be useful.
Also, may be useful as well, a GUI application that lets you test out the line counter.
Update
After further invetigation, I’ve noticed calculator was still having problems and needed a bit of customization. So I’ve improved calculation mechanism to provide details with text breaks composed inside.
This mechanism works most of the time. I’ve noticed couple of cases, where
JTextArea
would wrap with empty line, which was not detected. So use the code at your own risk.