2012-05-14 7 views
10

、これらは同じサイズでなければなりませんが、彼らはありませんね。NSStringよりも少し小さいコアテキストdrawInRect :?理論的には

青のテキストは黒で、コアのテキストからですが-[NSString drawInRect:]からです。コードは次のとおりです。

//Some formatting to get the correct frame 
int y = MESSAGE_FRAME.origin.y + 8; 

    if (month) y = y + 27; 

    int height = [JHomeViewCellContentView heightOfMessage:self.entry.message]; 

    CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height); 


    //Draw in rect method 
    UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f]; 

    [[UIColor colorWithWhite:0.2 alpha:1.0] setFill]; 

    [self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 



    //Frame change to suit CoreText 
    CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height, MESSAGE_FRAME.size.width - 16, height); 

    //Core text method 
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Crimson", 15.0f, NULL); 

    NSDictionary *attDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
            (__bridge_transfer id)fontRef, (NSString *)kCTFontAttributeName, 
            (id)[[UIColor blueColor] CGColor], (NSString *)kCTForegroundColorAttributeName, 
            nil]; 

    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:self.entry.message attributes:attDictionary]; 

    //Flip the coordinate system 
    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
    CGContextTranslateCTM(context, 0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 



    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, rect2); 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFAttributedStringRef)attString); 
    CTFrameRef theFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attString length]), path, NULL); 
    CFRelease(framesetter); 
    CFRelease(path); 

    CTFrameDraw(theFrame, context); 
    CFRelease(theFrame); 

フォントは同じです。私はなぜ人が違うようにレンダリングされているのか分かりません。

+0

サイズは異なるが、行間が異なるように見えます。 –

+0

http://stackoverflow.com/questions/10067389/giving-framesetter-the-correctline-spacing-adjustment – bshirley

+0

あなたはそうです。どうやって同じ行間隔にするのですか? – Andrew

答えて

関連する問題