Core Text入門

12219 ワード

home list tags talk user rss Core Text   
                  https://github.com/kejinlu/CTTest,      ,  Mac NSTextView     ,  iOS Core Text     

NSTextView Attribued String

                 Mac            ,               ,            ,  Mac AppKit    NSTextView            。                    ?   NSAttributedString,          AttributedString   。NSAttributedString NSString    Attribute   ,  NSAttributedString          ,               ,      NSRange      。     NSTextView        

NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"       "] autorelease];
//         
[attributedString addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:24] range:NSMakeRange(0, [attributedString length])];
// “  ”           
[attributedString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:NSMakeRange(0, 2)];
// “   ”            
[attributedString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(2, 3)];
   
// “ ” “ ”          
NSString *imageName = @"taobao.png";
NSFileWrapper *imageFileWrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:[[NSImage imageNamed:imageName] TIFFRepresentation]] autorelease];
imageFileWrapper.filename = imageName;
imageFileWrapper.preferredFilename = imageName;
    
NSTextAttachment *imageAttachment = [[[NSTextAttachment alloc] initWithFileWrapper:imageFileWrapper] autorelease];
NSAttributedString *imageAttributedString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attributedString insertAttributedString:imageAttributedString atIndex:1];

/*
           attributedString      1    8,                             
Printing description of attributedString:
 {
    NSColor = "NSCalibratedRGBColorSpace 0 0 1 1";
    NSFont = "\"LucidaGrande 24.00 pt. P [] (0x10051bfd0) fobj=0x101e687f0, spc=7.59\"";
}{
    NSAttachment = " \"taobao.png\"";
} {
    NSColor = "NSCalibratedRGBColorSpace 0 0 1 1";
    NSFont = "\"LucidaGrande 24.00 pt. P [] (0x10051bfd0) fobj=0x101e687f0, spc=7.59\"";
}   {
    NSColor = "NSCalibratedRGBColorSpace 1 0 0 1";
    NSFont = "\"LucidaGrande 24.00 pt. P [] (0x10051bfd0) fobj=0x101e687f0, spc=7.59\"";
}  {
    NSFont = "\"LucidaGrande 24.00 pt. P [] (0x10051bfd0) fobj=0x101e687f0, spc=7.59\"";
}
*/

[_textView insertText:attributedString];

    NSAttributedString              ,         size          (boundingRectWithSize:options:)          。             Mac              ,   Attributed String      ,        NSTextView   ,                 。   iOS           ,  iOS 3。2      NSAttributedString,       NSTextView          Attributed String。          Core Text 。

Core Text

     Core Text         iOS   。 Core Text  Core Graphics     ,    UIView drawRect    Graphics Context      。  Core Text            ,             ,                。

 .Core Text    

               ,                  。             ,               ;            ,                   。           。                 。        ,            。 Core Text            ,                。

1.  (Character)   (Glyphs)
                           ,          ,           ,             。                ,         ,  Unicode   ,            。        ,           ,        ,           。             (     ,           :     );             ,       ( Ligatures)。 
 
Roman Ligatures

                         Glyph Metrics

bounding box(    bbox),         ,           。
baseline(  ),      ,                   ,                   ,
ascent(    )         (               )         ,ascent     
descent(    )                 ,descent     (                     2,  descent  -2)
linegap(  ),linegap     leading(          External leading),  lineHeight      ascent + |descent| + linegap    。
  Metrics         Free Type    Glyph metrics,  iOS    Free Type         。

                Querying Font Metrics ,Text Layout

2.   
                    ,          。    Mac            ,  NSView      ,       。 Mac   View                 , NSTableView             。iOS UIKit UIView          。 
    ,Core Graphics context              。  iOS            Core Graphics   ,             ?  UIView drawRect         UIGraphicsGetCurrentContext()      Graphics Context。drawRect       ,  Graphics Context       ,       。     ,  CGContextGetCTM(CGContextRef c)            CGAffineTransformIdentity,          

Printing description of contextCTM:
(CGAffineTransform) contextCTM = {
        a = 1
        b = 0
        c = 0
        d = -1
        tx = 0
        ty = 460
}       
   retina       ,      retina   a,d,ty     2,   iPhone 5,ty      。                        flip,              ,y          。

       ,      ,Core Text               ,                ,                          。   iOS UIView drawRect   context    flip,         ,     context   Core Text  ,              。 
 
   UIView drawRect    context   Core Text       context    Flip。 

         CGContextSetTextMatrix,                      。

3.NSMutableAttributedString   CFMutableAttributedStringRef
Core Foundation Foundation                           ,          Toll-Free Bridged Types。 
CFMutableAttributedStringRef NSMutableAttributedString       ,Core Foundation      C   ,    ,        Foundation    Objc       ,                    ,              。

 .Core Text    

       Core Text          ,   Core Text     : 

framesetter framesetter       CTFramesetter,  CFAttributedString     ,   CTFrame       ,    path     CTFrame
CTFrame CTFrame     CTFrameDraw       context  ,          ,  CTFrame  CTLine,         
CTLine     Core Text                     line ascent,line descent ,line leading,     Line    Glyph Runs
CTRun      Glyph Run,        attributes(  )       
                      ,   ,Core Text          ,          Core Graphics   。  Core Text    CTRun                        。        CTRunDelegate ,                 ,CTRunDelegate  CTRun              ,       CTRun         。                  CTRun,    ascent,descent,width   ,                     。               Core Graphics      。 
  CTRunDelegateCreate      CTRunDelegate,       ,   callbacks   ,     callback            。 callbacks     CTRunDelegateCallbacks,           ,       run ascent,descent,width        ,              run,   CTRunDelegateCreate           ,  CTRunDelegateCreate                    。

 .Core Text  

    Core Text       NSTextView            。

           :

void RunDelegateDeallocCallback( void* refCon ){
   
}

CGFloat RunDelegateGetAscentCallback( void *refCon ){
    NSString *imageName = (NSString *)refCon;
    return [UIImage imageNamed:imageName].size.height;
}

CGFloat RunDelegateGetDescentCallback(void *refCon){
    return 0;
}

CGFloat RunDelegateGetWidthCallback(void *refCon){
    NSString *imageName = (NSString *)refCon;
    return [UIImage imageNamed:imageName].size.width;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //           drawRect context    
    CGContextSetRGBFillColor (context, 1, 0, 0, 1);
    CGContextFillRect (context, CGRectMake (0, 200, 200, 100 ));
    CGContextSetRGBFillColor (context, 0, 0, 1, .5);
    CGContextFillRect (context, CGRectMake (0, 200, 100, 200));
    
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);//         CGAffineTransformIdentity,                
    
    CGAffineTransform flipVertical = CGAffineTransformMake(1,0,0,-1,0,self.bounds.size.height);
    CGContextConcatCTM(context, flipVertical);//   context      flip
    
    NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"       "] autorelease];
    
    //         
    //[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, [attributedString length])]; // 6.0+
    UIFont *font = [UIFont systemFontOfSize:24];
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
    [attributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)fontRef range:NSMakeRange(0, [attributedString length])];
    
    // “  ”           
    //[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 2)]; //6.0+
    [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:NSMakeRange(0, 2)];
    
    // “   ”            
    //[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)]; //6.0+
    [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(2, 3)];
    
    //     CTRunDelegate,delegate           
    NSString *taobaoImageName = @"taobao.png";
    CTRunDelegateCallbacks imageCallbacks;
    imageCallbacks.version = kCTRunDelegateVersion1;
    imageCallbacks.dealloc = RunDelegateDeallocCallback;
    imageCallbacks.getAscent = RunDelegateGetAscentCallback;
    imageCallbacks.getDescent = RunDelegateGetDescentCallback;
    imageCallbacks.getWidth = RunDelegateGetWidthCallback;
    CTRunDelegateRef runDelegate = CTRunDelegateCreate(&imageCallbacks, taobaoImageName);
    NSMutableAttributedString *imageAttributedString = [[NSMutableAttributedString alloc] initWithString:@" "];//          
    [imageAttributedString addAttribute:(NSString *)kCTRunDelegateAttributeName value:(id)runDelegate range:NSMakeRange(0, 1)];
    CFRelease(runDelegate);
    
    [imageAttributedString addAttribute:@"imageName" value:taobaoImageName range:NSMakeRange(0, 1)];
    
    [attributedString insertAttributedString:imageAttributedString atIndex:1];
    
    CTFramesetterRef ctFramesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)attributedString);
    >
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect bounds = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height);
    CGPathAddRect(path, NULL, bounds);
    
    CTFrameRef ctFrame = CTFramesetterCreateFrame(ctFramesetter,CFRangeMake(0, 0), path, NULL);
    CTFrameDraw(ctFrame, context);
    
    CFArrayRef lines = CTFrameGetLines(ctFrame);
    CGPoint lineOrigins[CFArrayGetCount(lines)];
    CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), lineOrigins);
    
    for (int i = 0; i < CFArrayGetCount(lines); i++) {
        CTLineRef line = CFArrayGetValueAtIndex(lines, i);
        CGFloat lineAscent;
        CGFloat lineDescent;
        CGFloat lineLeading;
        CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, &lineLeading);
        
        CFArrayRef runs = CTLineGetGlyphRuns(line);
        for (int j = 0; j < CFArrayGetCount(runs); j++) {
            CGFloat runAscent;
            CGFloat runDescent;
            CGPoint lineOrigin = lineOrigins[i];
            CTRunRef run = CFArrayGetValueAtIndex(runs, j);
            NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);
            CGRect runRect;
            runRect.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0,0), &runAscent, &runDescent, NULL);
            
            runRect=CGRectMake(lineOrigin.x + CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL), lineOrigin.y - runDescent, runRect.size.width, runAscent + runDescent);
            
            NSString *imageName = [attributes objectForKey:@"imageName"];
            //      
            if (imageName) {
                UIImage *image = [UIImage imageNamed:imageName];
                if (image) {
                    CGRect imageDrawRect;
                    imageDrawRect.size = image.size;
                    imageDrawRect.origin.x = runRect.origin.x + lineOrigin.x;
                    imageDrawRect.origin.y = lineOrigin.y;
                    CGContextDrawImage(context, imageDrawRect, image.CGImage);
                }
            }
        }
    }
    
    CFRelease(ctFrame);
    CFRelease(path);
    CFRelease(ctFramesetter);
}

合計図面:
Core Text入門.png