如何获取固定宽度的文本高度并获取适合框架的文本长度?
好吧,我已经设法在标题中解决了我的所有问题。我需要将长文本分成列/框架并将其布局以供查看。我已经寻找解决方案几天了,但我找不到任何示例或清晰的文档来说明如何完成我的任何任务。我看到一些关于 StaticLayout 的提及,但我不知道如何正确使用它。至于文本高度,我尝试过 TextPaint 的 getTextBounds 方法,但它没有宽度限制,而且看起来它只测量单行(好吧,也许我做错了什么)。
也许有人有 StaticLayout 或其子类用法的示例?
一切在“纸上”看起来都很简单:创建“框架”,检查其中适合多少字符,填充框架并定位它,重复直到文本结束,但我找不到任何关于如何执行此操作的信息:)
well, I've managed to fit all my questions in the title. I need to break a long text in to columns/frames and layout them in to view. I've been digging for solutions for a couple of days now, but I can't find any examples or clear documentation on how to complete any of my tasks. I've seen a some mentions of StaticLayout, but I don't know how to use it properly. As for text height I've tried TextPaint's getTextBounds method, but it doesn't have width limit and it looks like it measures only single line (well, maybe I was doing something wrong).
Maybe someone has an example of StaticLayout or it's subclass usage?
Everything looks so simple "on paper": create "frame", check how much characters fits in it, fill frame and position it, repeat until end of text and yet I can't find anything on how to do this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道您问题的确切答案,但通常您可以使用图形库中提供的方法根据字体类型和大小计算文本的宽度和高度。
我已经用 C# 和 Java 完成了它。在 C# 中称为“MeasureString”,在 Java 中称为“FontMetrics”。
编辑:
看看这段代码是否有用(我没有编译它,因为我这里没有 android SDK):
Android 也有类似的方法:
测量文本,如果测量的宽度超过 maxWidth,则提前停止。< /em>
返回文本的宽度。
http: //developer.android.com/reference/android/graphics/Paint.html
I don't know the exact answer to your question, but generally you can calculate the width and height of a text based on the font type and size using methods available in the graphical library.
I've done it in C# and Java. in C# it's called "MeasureString", and in Java, "FontMetrics".
EDIT:
See if this code is useful ( I haven't compiled it because I don't have android SDK here):
Similar methods are available for Android:
Measure the text, stopping early if the measured width exceeds maxWidth.
Return the width of the text.
http://developer.android.com/reference/android/graphics/Paint.html