Coretext in iOS development

Source: Internet
Author: User

Source: http://iosdeveloper.diandian.com/post/2012-11-16/40041182749

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.

  • In core graphics/quartz, you can do everything the system can do very beautifully, but you must calculate the position of each character by yourself, then render it to the screen. That is to say, it cannot be used for Text Formatting.

  • Core text 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.

  • The core text object model briefly introduces 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

    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.

 

 

Let's take a look at some of the frequently used coretext classes on the iOS platform.


Nsattributedstring cfattributedstring a Text Segment with its own style (uniform and immutable)
Nsmutableattributedstring cfmutableattributedstring a Text Segment with multiple styles

Ctframe
1. cgpath-> cgpathgetboundingbox
2. ctframegetlineorigins can be used to obtain the starting position of each line.
3. You can also control text horizontal and vertical la S.

Ctframesetter
1. ctframesettersuggestframesizewithconstraints determines the size of the area required for text or the number of text displayed in the specified area

Ctline
1. ctlinegetstringindexforposition: determines the text range in the region's interior point.

Nstextcheckingresult indicates various links, custom time addresses, telephone URLs, and so on.

Nsdatadector
This is a class specifically used with nstextcheckingresult to check whether it is a specific link. It inherits from the nsregularexpression class.
Enumerate Traversal
Match matching Array
Number of numbersofmatch matches


1. Use core text to display a string first, and then define the style of each part of the string-> attributedstring-> Generate ctframesetter-> Get ctframe-> draw
You can set the line feed, alignment, and area size in detail.
2. The painting is only for display. What about Click events? You need a judgment.
Ctframe contains multiple ctlines and obtains the actual location and size of each line. Determine that the click is not on a line. Ctline can also determine the text range at this point (relative to the ctline coordinate. Traverse all nstextcheckingresults of the string, and judge that the click is not on this rang Based on the rang of the result to obtain the link and position of the click. Processing.

 

 

 

Transferred from: pjk1129

Related Article

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.