2011-02-16 4 views
4

내 코드는이 텍스트를 거꾸로 그립니다. 왜?iosc의 QuartzCore 묘사 문제

- (void)drawRect:(CGRect)rect { 
    // Drawing code. 

CGContextRef myContext = UIGraphicsGetCurrentContext(); 
CGRect contextRect = self.bounds; 

UIGraphicsPushContext(myContext); 



    CGContextSelectFont (myContext, // 3 
        "Courier", 
        24, 
        kCGEncodingMacRoman); 
    CGContextSetCharacterSpacing (myContext, 1); // 4 
    CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5 

    CGContextSetRGBFillColor (myContext, 0, 1, 0, 1.0); // 6 
    CGContextSetRGBStrokeColor (myContext, 1.0, 1.0, 1, 1); // 7 
    CGContextShowTextAtPoint (myContext, 100, 50, "Quartz 2D", 9); 

} 

답변

5

답변은 CoreGraphics가 포스트 스크립트 규칙 인 왼쪽 아래 모서리의 원점을 갖는 좌표계를 사용한다는 것입니다. 그러나 iOS에서는 왼쪽 위 모서리에 원점이 있습니다.

CoreGraphics를 사용하려면 좌표계를 뒤집고 원점을 프레임의 위쪽 가장자리로 이동해야합니다.

다음은 drawRect 메소드의 모양입니다.

- (void)drawRect:(CGRect)rect { 
    // Drawing code. 

    CGContextRef myContext = UIGraphicsGetCurrentContext(); 
    CGRect contextRect = self.bounds; 
    // flip transform used to transform the coordinate system from origin 
    // top left corner to bottom right corner and inverting the y-axis 
    CGAffineTransform flipTransform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(0.f, contextRect.size.height), 
                   CGAffineTransformMakeScale(1.f, -1.f)); 
    CGContextConcatCTM(myContext, flipTransform); 

    UIGraphicsPushContext(myContext); 



    CGContextSelectFont (myContext, // 3 
        "Courier", 
        24, 
        kCGEncodingMacRoman); 
    CGContextSetCharacterSpacing (myContext, 1); // 4 
    CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5 

    CGContextSetRGBFillColor (myContext, 0, 1, 0, 1.0); // 6 
    CGContextSetRGBStrokeColor (myContext, 1.0, 1.0, 1, 1); // 7 
    CGContextShowTextAtPoint (myContext, 100, 50, "Quartz 2D", 9); 

} 
당신은 텍스트 변환을 설정하는 별도의 기능을 사용한다
+0

: CGContextSetTextMatrix() –

+0

bleh http://stackoverflow.com/questions/3440825/does-cgcontextsettextmatrix-work-for-offscreen-bitmaps –

+0

왜 그래야 하나? 두 개의 CTM을 연결하여 작동합니다. 텍스트 그리기는 문제가되지 않았습니다. – GorillaPatch