创建格式化文本行
我需要能够读取使用 XML 进行格式化标记的文本,并将其显示在格式化的 iPad 上。一个技巧是我需要支持脚注。
我需要能够读取源代码、解析源代码、为源代码创建属性字符串、创建行。然后我需要在文本或脚注中写入行,直到填满屏幕。
有人告诉我核心文本是我需要使用的。然而,就像苹果的其他文档一样,Core Text 文档简直令人尴尬。
有一个框架设置器类,可以格式化文本以适合框架(似乎不是我想要做的)。 有一个排版程序类可以格式化行。但似乎没有办法指定线的宽度。
考虑到需要执行此操作的各种应用程序,我找不到任何示例。除了苹果的“文档”之外,一些仅以最模糊的术语“记录”的核心文本功能不会产生任何谷歌点击。
有人能解释一下如何完成这种编程品味吗?
谢谢
I need to be able to read text that has been tagged using XML for formatting and display it on the iPad formatted. The one trick is that I need to support footnotes.
I need to be able to read source, parse source, create attributed string for source, create lines. Then I need to write lines to either the text or footnotes until the screen is filled.
I am told that core text is what I need to use. However, like the rest of Apple's documentation, the Core Text documentation is simply embarrassing.
There is a framesetter class that formats text to fit in a frame (appears not to be what I want to do).
There is a typesetter class that formats lines. But there appears to be no way to specify the width of the line.
Considering the variety of applications that would need to do exactly this, I cannot find any examples whatsoever. Some of the core text functions that are only "documented" in the vaguest terms generate no Google hits except for the apple "documentation."
Can someone explain how one would accomplish this programming tast?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多潜在的方法,但其中一种是使用 CTTypeSetter。您缺少的详细信息是 CTTypeSetterSuggestLineBreak 和 CTTypeSetterSuggestLineBreakWithOffset 函数。这些确实需要宽度参数。
另一种方法是使用 CTFrameSetter,它只是在内部使用 CTTypeSetter。然后,您可以在从帧设置器获取的帧上调用
CTFrameGetLines
,并使用CTLineDraw
单独渲染线条。使用 CGContextSetTextPosition 告诉 Core Graphics 在哪里绘制每条线。如果您想找出原始字符串中每行代表哪些字符,可以使用CTLineGetStringRange
。由于听起来您是 Core Text 的新手,请注意,其坐标系的原点位于屏幕的左下角,并且 Y 轴随着您在屏幕上向上移动而增加。 UIKit 的其余部分使用相反的约定(原点位于左上角,Y 轴随着向下而增加)。您需要使用 CGContextScaleCTM( context, 1, -1 ) 和 CGContextTranslateCTM 来纠正这一问题。您可能还需要调用 CGContextSetTextMatrix( context, CGAffineTransformIdentity ) 以使文本按预期呈现。
There are a bunch of potential approaches, but one would be to use CTTypeSetter. The detail you're missing is the CTTypeSetterSuggestLineBreak and CTTypeSetterSuggestLineBreakWithOffset functions. Those do take a width argument.
Another approach is to use CTFrameSetter, which just uses a CTTypeSetter internally anyway. You can then call
CTFrameGetLines
on the frame you get from the frame setter and render the lines individually withCTLineDraw
. UseCGContextSetTextPosition
to tell Core Graphics where to draw the each line. If you want to figure out which characters in your original string are represented by each line, you can useCTLineGetStringRange
.Since it sounds like you're new to Core Text, note that its coordinate system has the origin at the bottom left corner of the screen and the Y axis increases as you go up the screen. The rest of UIKit uses the opposite convention (origin at top left, Y axis increases as you go down). You'll want to use
CGContextScaleCTM( context, 1, -1 )
andCGContextTranslateCTM
to correct for that. You'll also probably want to callCGContextSetTextMatrix( context, CGAffineTransformIdentity )
to make text render as expected.