1. coretext and uiwebview
Compared with the Implementation Based on coretext and uiwebview, the former has the following benefits:
- Coretext occupies less memory and uiwebview occupies more memory.
- Coretext can accurately obtain the height of the displayed content before rendering the interface (as long as a ctframe is available), and uiwebview can only obtain the height of the displayed content after rendering the content, to get the height of the content (and JavaScript code is also required)
- The ctframe of coretext can be rendered in the background thread, and the content of uiwebview can only be rendered in the main thread (ui thread.
- Based on coretext, you can achieve better native interaction and more delicate interaction effects. The interaction effects of uiwebview are all implemented using JavaScript, and some slow interaction effects exist. For example, in uiwebview, a simple button cannot achieve real-time and fine-grained native button pressing.
Of course, coretext-based solutions also have some disadvantages:
- The content rendered by coretext cannot be copied as conveniently as that of uiwebview.
- For coretext-based formatting, you need to process the image formatting logic and support link clicking.
Core Text object model
You have created a ctframesetter to associate it with your nsattributedstring. The cttypesetter instance is automatically created to manage your fonts. Next, use ctframesetter to create one or more frames for text rendering.
When you create a frame, you specify a text range used in the rectangle of the frame. Core Text automatically creates a ctline for each line of text (note here) and creates multiple ctrun text segments. The text in each ctrun has the same format.
For example, core text may create a ctrun for several red words, other ctrun may include plain text, and some ctrun may be bold. Again, you should not directly create ctrun instances by yourself. core text uses the nsattributedstring attributes you provide to create ctrun instances.
Each ctrun object can use different attributes, so you can precisely control more attributes such as the padding, hyphen, width, and height.
2. Drawing with core text
In general, core text does not have the ability to draw images. However, because it is a layout engine, what it can do is to reserve a space for you to draw images in it. At the same time, because your code already has the drawrect: method, it is easy to draw an image. Let's take a look at how a reserved space works in the text: Do you still remember that all text blocks are actually ctrun instances? You only need to set the delegate for the ctrun, and the delegate object will be responsible for notifying the ctrun's rising space, descent space and width to the core text. For example:
To present an image, you need to clearly know which frame the image will be displayed in the application. To locate this origin, we need a series of values:
- Contentoffset
- Frame Offset of ctview (framexoffset, frameyoffset)
- The origin coordinate of the ctline (the ctline may be offset at the beginning of the paragraph)
- The last is the distance between the ctrun and ctline origins.
Iii. Character & glyph
A character is the smallest unit of written language that carries meaning, e.g. uppercase.
A glyph is a concrete form of a character. in our previous example, the character uppercase A can be drawn with a different size or a different stroke thickness. it can lean or be vertical, and have certain optional variations in form. the following figure represents glyphs of the character:
Note that characters and glyphs do not have a one-to-one corresponsor. in some cases, a character may be represented by multiple glyphs, such as an é, which may be an e glyph combined with an acute accent glyph' (ACCENT ). in other cases, a single glyph may represent multiple characters, as in the case of a ligature, or joined letter. the following figure represents the ligature case:
A glyph-run is a set of consecutive glyphs sharing the same attributes and direction.
Refer:
1. http://www.dapps.net/dev/iphone/how-to-create-a-simple-magazine-app-with-core-text.html
2. http://weblog.invasivecode.com/core-text
Coretext Study Notes