Core Text object model

Source: Internet
Author: User

Core text is a text rendering engine for iOS 3.2 + and OSX 10.5 +, allowing you to freely control the text format and layout.

Core text is different from uikit and core graphics/quartz (although the latter two can also be used for text rendering ):

  • In uikit, you can use uilabel to display text. Its operation is very simple, but you cannot control the font color of a single character in uilabel. That is to say, there is no way to display rich text.
  • InCore graphics/quartzYou can do everything the system can do very beautifully, but you must calculate the position of each character on your own and then render it to the screen. That is to say, it cannot be used for Text Formatting.
  • Core textIt is the combination of the above two points. You can control attributes such as the text position, layout, color, size, and so on without worrying about character positions, broken lines, and other things.
  • 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.