CTTypesetter
First, let's take a look at the overall window combination diagram of the framework:
As a Canvas, CTFrame consists of rows, and each row can be divided into one or more small squares (CTRun ).
Note: 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.
Usually process steps:
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 (CTFrameDraw)
You can set the line feed, alignment, and area size in detail.
2. The painting is only displayed, and the click event requires 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.
Basic font knowledge:
Font: A series of characters with the same Font size, style, and pound value (for example, Palatino, a 10-pound simhei ). Most of them are considered synonyms of words.
Face: a combination of the scales and formats of all font sizes
Font family: a set of related fonts (for example, Franklin family includes Franklin Gothic, Fran-klinHeavy, and Franklin Compressed)
Weight: used to describe the font width. Typical pounds, ranging from the shortest to the shortest, are extremely fine, fine, book, moderate, semi-coarse, and extremely crude.
Style: Roman type is a straight body, oblique type is italic, and utakuc type is italic and curve (more like a calligraphy body than Roman type ).
X height: the average height of lowercase letters (based on X ). Two letters with the same pound value, the letter with the higher x height looks bigger than the letter with the lower x height
Cap height: similar to x. The average height of uppercase letters (based on C)
Descender: for example, in the letter q, the lower part of the baseline is the lower part.
Ascender: the upper and lower part of x (such as letter B) is called the upper and lower part.
Baseline: The line usually under x, v, B, and m.
Stroke: a line or curve that constitutes a character. Bold or character shapes can be changed
Serif: a horizontal line used to make characters more visible. For example, the upper left corner and lower part of the letter are horizontal lines.
Sans Serif: The Sans Serif can be used by the typesetting staff not to deploy the Sans Serif.
Block: a stroke in this font makes the character look more conspicuous than the silent line, but it is less than the degree of common lines. For example, Lubalin Graph is a square word, which looks like wood blocks.
Calligraphic script: a font used to simulate handwriting. For example, Murray Hill or Fraktur font
Decorative: A painting-like font
Pi (Pisymbol): a special symbol for non-standard alphanumeric characters. For example, Wingdings and Mathematical Pi
Ligature: A series of writing letters, such as fi, fl, ffi, or ffl. Because the characters are often in the shape of letters, the typesetting staff are used to connecting them.
Character attribute name:
const CFStringRef kCTCharacterShapeAttributeName;
// The font shape attribute must be a CFNumberRef object. The default value is 0. If the value is not 0, the corresponding character shape is defined. For example, 1 indicates the traditional character shape.
const CFStringRef kCTFontAttributeName;
// The font attribute must be the CTFont object const CFStringRef kCTKernAttributeName;
// The character interval attribute must be the CFNumberRef object const CFStringRef kCTLigatureAttributeName;
// Set whether to use the hyphen property. If it is set to 0, the hyphen property is not used. The standard English hyphen (FI) and FL. The default value is 1. Both use the standard hyphen. That is, when f is searched, fl is considered as a text. It must be CFNumberRef. The default value is 1. The value 0, 1, and 2 const CFStringRef kCTForegroundColorAttributeName can be obtained;
// The font color attribute must be a CGColor object. The default value is blackconst CFStringRef kCTForegroundColorFromContextAttributeName;
// The context font color attribute must be CFBooleanRef; default value: False; const CFStringRef kCTParagraphStyleAttributeName;
// The Section style attribute must be a CTParagraphStyle object. The default value is NILconst CFStringRef kCTStrokeWidthAttributeName;
// The stroke line width must be a CFNumberRef object. The default value is 0.0f. The standard value is 3.0 fconst CFStringRef kCTStrokeColorAttributeName;
// The color attribute of a stroke must be a CGColorRef object. The default value is const CFStringRef kCTSuperscriptAttributeName;
// Set the font's upper/lower mark attribute to 0 by default for the CFNumberRef object. It can be-1 as the subscript, and 1 as the upper mark, which must be supported by the font. For example, Cn1const CFStringRef kCTUnderlineColorAttributeName;
// The font underline color attribute must be a CGColorRef object. The default value is const CFStringRef kCTUnderlineStyleAttributeName;
// The font underline style attribute must be a CFNumberRef object. If it is kCTUnderlineStyleNone, you can use CTUnderlineStypleModifiers to modify the underline style const CFStringRef kCTVerticalFormsAttributeName;
// The font direction attribute of the text must be CFBooleanRef. The default value is false. false indicates the horizontal direction. true indicates the const CFStringRef kCTGlyphInfoAttributeName in the vertical direction;
// The font information attribute must be the CTGlyphInfo object const CFStringRef kCTRunDelegateAttributeName
// The CTRun delegate attribute must be a CTRunDelegate object.
Example:
[Cpp]View plaincopy
- NSMutableAttributedString * mabstring = [[NSMutableAttributedString alloc] initWithString: @ This is a test of characterAttribute. Chinese character];
[Cpp]View plaincopy
- // Set Font attributes
- CTFontRef font = CTFontCreateWithName (CFSTR (Georgia), 40, NULL );
- [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
[Cpp]View plaincopy
- // Set italics
- CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 14, NULL );
- [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
[Cpp]View plaincopy
- // Underline
- [Mabstring addattriename :( id) kCTUnderlineStyleAttributeName value :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] range: NSMakeRange (0, 4)];
[Cpp]View plaincopy
- // Underline color
- [Mabstring addattriename :( id) kCTUnderlineColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 4)];
[Cpp]View plaincopy
- // Set the font to simple separated by eg: test
- Long number = 10;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addattriange :( id) kCTKernAttributeName value :( id) num range: NSMakeRange (10, 4)];
[Cpp]View plaincopy
- // Set the hyphen
- Long number = 1;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addAttribute :( id) kCTLigatureAttributeName value :( id) num range: NSMakeRange (0, [str length])]; The hyphen is not used yet and no effect is displayed.
[Cpp]View plaincopy
- // Set the font color
- [Mabstring addattriename :( id) kCTForegroundColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 9)];
[Cpp]View plaincopy
- // Set the font color to the prefix color.
- CFBooleanRef flag = kCFBooleanTrue;
- [Mabstring addattriename :( id) kCTForegroundColorFromContextAttributeName value :( id) flag range: NSMakeRange (5, 10)]; no obvious effect.
[Cpp]View plaincopy
- // Set hollow words
- Long number = 2;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addAttribute :( id) kCTStrokeWidthAttributeName value :( id) num range: NSMakeRange (0, [str length])];
[Cpp]View plaincopy
- // Set hollow words
- Long number = 2;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addAttribute :( id) kCTStrokeWidthAttributeName value :( id) num range: NSMakeRange (0, [str length])];
-
- // Set the font color
- [Mabstring addattriename :( id) kCTStrokeColorAttributeName value :( id) [UIColor greenColor]. CGColor range: NSMakeRange (0, [str length])];
When you set the font color, you must first set the font height to hollow, or set the color to no effect.
[Cpp]View plaincopy
- // Set multiple attributes for the same font family
- // Red
- NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithObject :( id) [UIColor redColor]. CGColor forKey :( id) kCTForegroundColorAttributeName];
- // Italic
- CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 40, NULL );
- [Attributes setObject :( id) font forKey :( id) kCTFontAttributeName];
- // Underline
- [Attributes setObject :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] forKey :( id) kCTUnderlineStyleAttributeName];
-
- [Mabstring addAttributes: attributes range: NSMakeRange (0, 4)];
It's draw.
[Cpp]View plaincopy
- -(Void) characterAttribute
- {
- NSString * str = @ This is a test of characterAttribute. Chinese character;
- NSMutableAttributedString * mabstring = [[NSMutableAttributedString alloc] initWithString: str];
-
- [Mabstring beginEditing];
- /*
- Long number = 1;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addattriename :( id) kCTCharacterShapeAttributeName value :( id) num range: NSMakeRange (0, 4)];
- */
- /*
- // Set Font attributes
- CTFontRef font = CTFontCreateWithName (CFSTR (Georgia), 40, NULL );
- [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
- */
- /*
- // Set the font to simple separated by eg: test
- Long number = 10;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addattriange :( id) kCTKernAttributeName value :( id) num range: NSMakeRange (10, 4)];
- */
-
- /*
- Long number = 1;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addAttribute :( id) kCTLigatureAttributeName value :( id) num range: NSMakeRange (0, [str length])];
- */
- /*
- // Set the font color
- [Mabstring addattriename :( id) kCTForegroundColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 9)];
- */
- /*
- // Set the font color to the prefix color.
- CFBooleanRef flag = kCFBooleanTrue;
- [Mabstring addattriename :( id) kCTForegroundColorFromContextAttributeName value :( id) flag range: NSMakeRange (5, 10)];
- */
-
- /*
- // Set hollow words
- Long number = 2;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addAttribute :( id) kCTStrokeWidthAttributeName value :( id) num range: NSMakeRange (0, [str length])];
-
- // Set the font color
- [Mabstring addattriename :( id) kCTStrokeColorAttributeName value :( id) [UIColor greenColor]. CGColor range: NSMakeRange (0, [str length])];
- */
-
- /*
- Long number = 1;
- CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
- [Mabstring addAttribute :( id) kCTSuperscriptAttributeName value :( id) num range: NSMakeRange (3, 1)];
- */
-
- /*
- // Set italics
- CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 14, NULL );
- [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
- */
-
- /*
- // Underline
- [Mabstring addattriename :( id) kCTUnderlineStyleAttributeName value :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] range: NSMakeRange (0, 4)];
- // Underline color
- [Mabstring addattriename :( id) kCTUnderlineColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 4)];
- */
-
-
-
- // Set multiple attributes for the same font family
- // Red
- NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithObject :( id) [UIColor redColor]. CGColor forKey :( id) kCTForegroundColorAttributeName];
- // Italic
- CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 40, NULL );
- [Attributes setObject :( id) font forKey :( id) kCTFontAttributeName];
- // Underline
- [Attributes setObject :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] forKey :( id) kCTUnderlineStyleAttributeName];
-
- [Mabstring addAttributes: attributes range: NSMakeRange (0, 4)];
-
-
-
- Nsange kk = NSMakeRange (0, 4 );
-
- NSDictionary * dc = [mabstring attributesAtIndex: 0 paitiverange: & kk];
-
- [Mabstring endEditing];
-
- NSLog (@ value = % @, dc );
-
-
-
- CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString (CFAttributedStringRef) mabstring );
-
- CGMutablePathRef Path = CGPathCreateMutable ();
-
- CGPathAddRect (Path, NULL, CGRectMake (10, 0, self. bounds. size. width-10, self. bounds. size. height-10 ));
-
- CTFrameRef frame = CTFramesetterCreateFrame (framesetter, CFRangeMake (0, 0), Path, NULL );
-
- // Obtain the current (View) context to facilitate subsequent painting. This is an off-screen.
- CGContextRef context = UIGraphicsGetCurrentContext ();
-
- CGContextSetTextMatrix (context, CGAffineTransformIdentity );
-
- // Press the stack and press it into the graphical state stack. Each graphic context maintains a graphical state stack. Not all elements of the graphics state of the current painting environment are saved. The current path is not considered in the graph status, so it is not saved.
- // Save the current context image status. No matter what you draw on the context in the future, the real screen will not be affected.
- CGContextSaveGState (context );
-
- // X and Y-axis move
- CGContextTranslateCTM (context, 0, self. bounds. size. height );
-
- // Scale x and Y axis,-1.0 is 1.0 times in reverse direction, coordinate system conversion, turning 180 degrees along the x axis
- CGContextScaleCTM (context, 1.0,-1.0 );
-
- CTFrameDraw (frame, context );
-
- CGPathRelease (Path );
- CFRelease (framesetter );
- }
[Cpp]View plaincopy
- -(Void) drawRect :( CGRect) rect
- {
- [Self characterAttribute];
- }
CORETEXT framework
For more information about Context, see: http://www.padovo.com/blog/2013/01/31/study-coretext/