如何使用CoreText在富文本中绘制图像? (iOS)
我可以使用核心文本绘制富文本,问题是将图像与文本一起放置。 (iOS SDK 4.1)
我尝试绘制某种富文本。问题是设计者在文本中放置了许多图标。所以我必须绘制的文本是这样的:
这里有一个词<图标图像>,还有另一个词。 图像(<另一个图标>)应像字形一样放置。 这是文本的一部分,而不是示例。
是图像。 (这不是代码。只是一个插图。)
我可以通过手动布局所有这些来绘制它,但是保持复杂的文本布局行为太难了。所以我正在寻找一种用核心文本来绘制它的方法。
I can draw rich-text with Core Text, the problem is placing images flowing with the text.
(iOS SDK 4.1)
I'm try to drawing some kind of rich-text. Problem is designer placed many icons among text. So the text what I have to draw is something like this:
Here is a word <an icon image>, and another words. The image(<another icon>) should be placed like a glyph. It's part of text, not an example.
<icon>
are images. (This is not a code. Just an illustration.)
I can draw this by laying out all of them manually, but it's too hard keeping complex text layout behaviors. So I'm finding a way to draw this with Core Text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了解决方案。
布局非文本内容的关键是
CTRunDelegate
。Core Text
不支持非文本内容,因此您必须为它们留出空白,然后自己绘制或放置它们。带有
kCTRunDelegateAttributeName
属性的NSAttributedString
的一部分将调用注册的回调来确定每个字形的宽度。这将让您为每个非文本对象留出空白空间。但是,使用
Core Text
绘制文本后,frame/line/run中存储的布局信息将失效。因此,您必须在使用框架排版机/排字机布局之后、绘制之前绘制/放置非文本内容。此链接描述了 CTRunDelegate 的基本用法:
如何在 iPad 中使用 CTRunDelegate?
Core Text 存在问题。最初,CTRunDelegate 设计为通过 CTRunDelegateCallbacks.getAscent 和 CTRunDelegateCallbacks.getDescent 支持可变宽度和垂直对齐。但垂直对齐功能目前无法使用。这可能是一个错误。
我在这里描述了这个问题:
对齐多个尺寸的文本垂直居中而不是 iOS 中 Core Text 的基线
如果您有有关此问题的信息,请参阅链接中的我的问题。
I got solution.
The key of laying out non-text content is
CTRunDelegate
.Core Text
does not support non-text content, so you have to make blank spaces for them, and draw or place them yourself later.A part of
NSAttributedString
attributed withkCTRunDelegateAttributeName
will call registered callback to determine width of each glyph. This will let you make blank space for each non-text object.However, after drawing the text with
Core Text
, the layout information stored with frame/line/run will invalidated. So you have to draw/place non-text contents after layout with framesetter/typesetter, but before drawing.This link describes basic usage of CTRunDelegate:
How to use CTRunDelegate in iPad?
There is a problem with Core Text. Originally, CTRunDelegate designed to support variable width and vertical alignment via
CTRunDelegateCallbacks.getAscent
andCTRunDelegateCallbacks.getDescent
. But vertical alignment feature doesn't work currently. This might be a bug.I described this problem here:
Aligning multiple sized text vertical center instead of baseline with Core Text in iOS
If you have informations about this problem, please see my question at the link.
您只需为给定的 CTRun 设置一个委托,委托对象负责让 Core Text 了解 CTRun 的上升空间、下降空间和宽度。
当核心文本“到达”具有 CTRunDelegate 的 CTRun 时,它会询问委托 - 我应该为这块数据保留多少宽度,应该有多高?通过这种方式,您可以在文本中挖一个洞,然后在该位置绘制图像。
这是一个关于 Core Text 的博客,它可以为您提供答案。
如何创建简单的带有核心文本的杂志应用
You simply set a delegate for a given CTRun and the delegate object is responsible to let know Core Text what is the CTRun ascent space, descent space and width.
When Core Text "reaches" a CTRun which has a CTRunDelegate it asks the delegate - how much width should I leave for this chunk of data, how high should it be? This way you build a hole in the text - then you draw your image in that very spot.
Here is a blog about Core Text.It has the answer for you .
How To Create a Simple Magazine App with Core Text