如何使用CoreText在富文本中绘制图像? (IOS) [英] How to draw images among rich-text with CoreText? (iOS)
问题描述
我可以使用Core Text绘制富文本,问题是将图像与文本一起流动。
(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>
是图片。 (这不是代码。只是一个例子。)
<icon>
are images. (This is not a code. Just an illustration.)
我可以通过手动布置所有这些来绘制它,但是保持复杂的文本布局行为太难了。所以我找到了一种用Core Text绘制它的方法。
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.
推荐答案
我得到了解决方案。
布置非文本内容的关键是 CTRunDelegate
。
核心文本
不支持非文本内容,因此您必须为它们创建空格,并在以后绘制或放置它们。
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.
归因于 kCTRunDelegateAttributeName
的 NSAttributedString
的一部分将调用已注册的回调来确定宽度每个字形。这将允许您为每个非文本对象创建空白。
A part of NSAttributedString
attributed with kCTRunDelegateAttributeName
will call registered callback to determine width of each glyph. This will let you make blank space for each non-text object.
然而,在使用核心文本
,使用frame / line / run存储的布局信息将无效。因此,您必须在使用framesetter / typesetter进行布局之后,但在绘制之前绘制/放置非文本内容。
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.
此链接描述了CTRunDelegate的基本用法:
如何在iPad中使用CTRunDelegate?
This link describes basic usage of CTRunDelegate:
How to use CTRunDelegate in iPad?
Core Text存在问题。最初,CTRunDelegate旨在通过 CTRunDelegateCallbacks.getAscent
和 CTRunDelegateCallbacks.getDescent
支持可变宽度和垂直对齐。但是垂直对齐功能目前不起作用。这可能是一个错误。
There is a problem with Core Text. Originally, CTRunDelegate designed to support variable width and vertical alignment via CTRunDelegateCallbacks.getAscent
and CTRunDelegateCallbacks.getDescent
. But vertical alignment feature doesn't work currently. This might be a bug.
我在这里描述了这个问题:
使用iOS中的核心文本对齐多个大小的文本垂直中心而不是基线
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.
这篇关于如何使用CoreText在富文本中绘制图像? (IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!