Core Text object model
Here we will briefly introduce some basic concepts of core text rendering to help you understand the working mechanism of coretext.
Is the core text object model:
We use nsattributedstring to create a ctframesetter. At this time, a cttypesetter instance is automatically created, which manages fonts. Next we use ctframesetter to create one or more frames to render text. Then core text will automatically create a ctline (each line corresponds to a ctline) and ctrun (one or more adjacent characters in the same format constitute a ctrun) based on the frame size ).
Original document explanation:
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-4 shows the conceptual hierarchy of a frame object containing line objects that, in turn, contain glyph-run objects.
Figure 1-4 A 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 (
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 varous 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.
For example, core text creates a ctrun to draw some red text, a ctrun to draw plain text, and a ctrun to draw bold text. Note that you do not need to create ctrun by yourself. core text will automatically create ctrun Based on the attributes of nsattributedstring. Each ctrun object corresponds to different attributes. Therefore, you can freely control the font, color, font spacing, and other information.