Introduction:

Read tang qiao’s advanced iOS development, notes on CoreText. Using CoreText technology, you can do complex typesetting of rich text. With some simple extensions, you can click on images and links.

CoreText introduction:

The Apple documentation says: “Create text layouts and optimize fonts

Process, and access font specifications and glyphs data. Provides a low-level programming interface for laying out text and manipulating fonts. The Core Text layout engine is designed for high performance, ease of use, and tight integration with Core Foundation. The text layout API provides high-quality typography, including character-to-glyph conversion, hyphenation, spacing adjustment, and more. Complementary Core Text font technology provides automatic font replacement (cascading), font descriptors and collections, easy access to font specifications and glyphs data, and many other features.”

CoreText is the underlying technology for processing text and fonts. It works directly with Core Graphics (Quartz). Quartz is a 2D graphics rendering engine that handles graphics rendering issues in OSX and iOS.

Quartz works directly with fonts (Font) and glyphs, rendering text to the interface, and is the only module in the base library capable of handling glyphs. CoreText therefore needs to pass the content, position, font, and glyphs of the displayed text directly to Quartz for typography purposes. Compared to other UI components, CoreText has efficient typography because it interacts directly with Quartz.

While CoreText can do complex page layout and render better, it doesn’t have its own response chain like UI components do, so we need to implement some of the complex logic ourselves.

Basic typesetting based on CoreText

Start by implementing a simple effect similar to UILabel’s text display. The effect is as follows:

Steps:

1. Open Xcode->Create a new Xcode project->iOS->Signle View App-> Set the project name, organization and other information -> select the storage location -> Open the project

2. Right-click on the ViewController and create a UIView subclass named KGDisPlayView, then override the – (void)drawRect:(CGRect)rect method in the KGDisPlayView.

3. The code is as follows:

- (void)drawRect:(CGRect)rect{ [super drawRect:rect]; / / get the current context CGContextRef context = UIGraphicsGetCurrentContext (); / / set the current text matrix, the specified text matrix conversion from text space to user space CGContextSetTextMatrix (context, CGAffineTransformIdentity); /* Change the origin of the user coordinate system in context * * CGContextRef C text context * CGFloat tx moves the amount of the x axis of the coordinate space in the specified context in user space * CGFloat TY in user space, The amount of moving the Y-axis of coordinate space in a given context * for the underlying drawing frame the bottom left corner of the screen is the origin, whereas in UIKit the top left corner of the screen is the origin, So here we do the coordinate flip */ CGContextTranslateCTM(context, 0, self.bound.size. Height); /* Change the scale of the user coordinate system in the context * CGContextRef C context * CGFloat sx is used to scale the factor of the x axis of the specified context coordinate space * CGFloat SY scales the factor of the y axis of the specified Contex coordinate space */ CGContextScaleCTM (context, 1.0, 1.0); CGMutablePathRef path = CGPathCreateMutable(); /* Append rectangle to variable graph path * CGMutablePathRef Path Variable path to change * const CGAffineTransform *m Pointer to affine transformation matrix, null if no transformation is required. If specified, Core Graphics applies a transform to the rectangle before adding it to the path * CGRect Rect the rectangle to be added */ CGPathAddRect(path, NULL, self.bounds); NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Hello World!"] ; // Create an immutable framesetter object from the attribute string CTFramesetterRef Framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); CTFrameRef Frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, [attString Length]), path, NULL); // Draw the entire frame into the context CTFrameDraw(frame, context); / / release CFRelease (frame); CFRelease(path); CFRelease(frameSetter); }Copy the code

4. Import the KGDisPlayView header file in the ViewController, then create the object, set the frame size, and add it to the View. The code is as follows:

self.ctView = [[KGDisPlayView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, 300)];
[self.view addSubview:self.ctView];
Copy the code

So this is the basic introduction to the use of CoreText and when we’re done, we run the program, and it looks exactly as it did at the beginning, we’ve done what we wanted to do.

Series of articles:

Simple Use of CoreText (PART 1)

Simple Use of CoreText (Part 2)

Simple Use of CoreText (PART 3)

Simple Use of CoreText (4)

Simple Use of CoreText (5)