`
yidongkaifa
  • 浏览: 4063088 次
文章分类
社区版块
存档分类
最新评论

CoreText

 
阅读更多

看一下iOS平台下CoreText的一些常用的类。


NSAttributedString CFAttributedString 一段有自己样式(统一不可变)的文字
NSMutableAttributedString CFMutableAttributedString 一段有多种样式的文字

CTFrame
1.CGPath -> CGPathGetBoundingBox 得到占用的区域大小
2.CTFrameGetLineOrigins 可以得到每行的起始位置
3.还可以控制文字横排竖排等

CTFramesetter
1.CTFramesetterSuggestFrameSizeWithConstraints 确定文字所需区域大小或指定区域可以显示多少文字

CTLine
1.CTLineGetStringIndexForPosition 确定区域内点处文字范围

NSTextCheckingResult 代表各种链接,时间地址电话网址自定义等。

NSDataDector
这是一个专门配合NSTextCheckingResult检测是否是特定链接的类。继承于NSRegularExpression类。
enumerate 遍历
match 匹配的数组
numbersOfMatch 匹配的个数


1.使用core text就是先有一个要显示的string,然后定义这个string每个部分的样式->attributedString -> 生成 CTFramesetter -> 得到CTFrame -> 绘制
其中可以更详细的设置换行方式,对齐方式,绘制区域的大小等。
2.绘制只是显示,点击事件呢?就需要一个判断了。
CTFrame 包含了多个CTLine,并且可以得到各个line的其实位置与大小。判断点击处在不在某个line上。CTLine 又可以判断这个点(相对于ctline的坐标)处的文字范围。然后遍历这个string的所有NSTextCheckingResult,根据result的rang判断点击处在不在这个rang上,从而得到点击的链接与位置。处理。

Core Text对象模型

Core Text是iOS 3.2+和OSX 10.5+的文本渲染引擎,可以让你自由的控制文本格式和排版。

Core Text不同于UIKit和Core Graphics/Quartz(虽然通过后两者你也可以进行文字渲染):

  • 在UIKit中,你可以使用UILabel来显示文字,它的操作非常简单,但是你没有办法控制UILabel中单独一个字符的字体颜色。也就是说,没有办法进行富文本的显示。
  • Core Graphics/Quartz中你可以非常漂亮的做系统所能做的每一件事,但是你必须要自己计算每个字符的位置,然后再把它渲染到屏幕上。也就是说它无法进行文字排版。
  • Core Text 正是以上两点的结合。你既可以控制文字的位置、布局、颜色、大小等等属性,又不需要实际操心字符位置、文字断行等琐事。
  • Core Text对象模型

    这里简单介绍一下Core Text渲染文本的一些基本概念,以便有助于理解CoreText的工作机制。

    下图是Core Text对象模型:



    我们通过NSAttributedString创建一个CTFramesetter,这时候会自动创建一个 CTTypesetter实例,它负责管理字体,下面通过CTFramesetter来创建一个或多个frame来渲染文字。然后Core Text会根据frame的大小自动创建CTLine(每行对应一个CTLine)和CTRun(相同格式的一个或多个相邻字符组成一个CTRun)。

    文档原文解释:

    A line object contains glyph-run objects, represented by the CTRun opaque type. A glyph run is a set of consecutive glyphs sharing the same attributes and direction. The typesetter creates glyph runs as it produces lines from character strings, attributes, and font objects. That is, a line is constructed of one or more glyphs runs. Glyph runs can draw themselves into a graphic context, if desired, although most clients have no need to interact directly with glyph runs.Figure 1-4shows the conceptual hierarchy of a frame object containing line objects that, in turn, contain glyph-run objects.

    Figure 1-4A frame object containing lines and glyph runs


    CTLine has a convenience method for creating a freestanding line independent of a frame,CTLineCreateWithAttributedString. You can use this method to create a line object directly from an attributed string without needing to create and manage a typesetter. Without a typesetter, however, there’s no way to calculate line breaks, so this method is meant for a single line only (for example, creating a text label).

    After you have a line object, you can do a number of things with it. For example, you can have the line create a justified or truncated copy of itself, and you can ask the line for pen offsets for various degrees of flushness. You can use these pen offsets to draw the line with left, right, or centered alignments. You can also ask the line for measurements, such as its image bounds and typographic bounds. Image bounds represent the rectangle tightly enclosing the graphic shapes of the glyphs actually appearing in the line. Typographic bounds include the height of the ascenders in the font and the depth of its descenders, regardless of whether those features appear in the glyphs in a given line.

    Like a frame object, a line object is ready to draw. You simply set the text position in a Core Graphics context and have the line draw itself. Core Text uses the same placement strategy as Quartz, setting the origin of the text on the text baseline.

    In Quartz, you specify the location of text in user-space coordinates. The text matrix specifies the transform from text space to user space. The text position is stored in the txand tyvariables of the text matrix. When you first create a graphics context, it initializes the text matrix to the identity matrix; thus text-space coordinates are initially the same as user-space coordinates. Quartz conceptually concatenates the text matrix with the current transformation matrix and other parameters from the graphics state to produce the final text-rendering matrix, that is, the matrix actually used to draw the text on the page.


    举例来说,Core Text将创建一个CTRun来绘制一些红色文字,然后创建一个CTRun来绘制纯文本,然后再创建一个CTRun来绘制加粗文字等等。要注意,你不需要自己创建CTRun,Core Text将根据NSAttributedString的属性来自动创建CTRun。每个CTRun对象对应不同的属性,正因此,你可以自由的控制字体、颜色、字间距等等信息。



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics