The main content of this paper is to discuss how to use CoreText for the simplest text content drawing, but also talked about CoreText drawing a most basic but also the most important concept of CoreText coordinate system, the concept of CoreText coordinate system is throughout all CoreText drawing scenes, So here’s an introduction

Other articles: CoreText Entry (1) – Text drawing CoreText entry (2) – Drawing pictures CoreText advanced (3) – Event processing CoreText Advanced (4) – Text line limit and display more CoreText advanced (5) – text typesetting style and effects CoreText Advanced (6) – Content size calculation and automatic layout CoreText Advanced (7) – Add custom View and to it

The main content of this article is as follows

  • What is CoreText
  • Coordinate system
  • Simple text drawing
  • conclusion

Demo: CoreTextDemo

What is CoreText

The apple documentation describes CoreText as follows

Core Text is an advanced, low-level technology for laying out text and handling fonts. Core Text works directly with Core Graphics (CG), also known as Quartz, which is the high-speed graphics rendering engine that handles two-dimensional imaging at the lowest level in OS X and iOS.

CoreText is an advanced low-level technology for laying out text and manipulating fonts. Working directly with Core Graphics (CG), also known as Quartz, CoreText is a high-speed Graphics rendering engine that handles 2d imaging at the bottom of OS X and iOS.

Coordinate system

The origin of coordinate system of UIKit is in the upper right corner, and the origin of coordinate system of CoreText is in the lower left corner, and the content drawn is upside down, so coordinate transformation is required to display the content drawn properly

Coordinate system


    CGContextRef context = UIGraphicsGetCurrentContext(a);CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0.self.bounds.size.height);
    CGContextScaleCTM(context, 1.- 1);
Copy the code

Step example figure: Gleason

Step example diagram

Simple text drawing

rendering

rendering

Text drawing flow chart: Gleason

Text drawing flow chart

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext(a);CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0.self.bounds.size.height);
    CGContextScaleCTM(context, 1.- 1);

    // Draw the region
    CGMutablePathRef path = CGPathCreateMutable(a);CGPathAddRect(path, NULL.self.bounds);

    // Draw the content property string
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18].NSForegroundColorAttributeName: [UIColor blueColor]
                                 };
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world" attributes:attributes];

    / / use NSMutableAttributedString CTFrame creation
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrStr);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrStr.length), path, NULL);

    // Draw on CGContextRef context using CTFrame
    CTFrameDraw(frame, context);
}
Copy the code

conclusion

Using CoreText to draw text steps are relatively simple, here face uses a CoreText class CTFrame, CoreText there are many other concepts are not covered, the next CoreText introduction (2) – drawing images will cover more concepts in CoreText