2014-04-21 4 views
3

IOS CGContext 문서에서는 현재 다양한 텍스트 출력 기능이 코어 텍스트를 사용하도록 권장되지 않습니다. 설명서에는 "대신 핵심 텍스트 사용"이라고 나와 있습니다.코어 텍스트를 사용하는 출력 문자열

내가 가지고있는 경우

NSString *string ; 

무엇 CGContext에 텍스트를 그리기위한 간단한, 현재 승인 된 방법이 될 것입니다?

+0

당신이 본 적이 [iOS 용 핵심 텍스트 자습서 : 만들기 매거진 앱] (http://www.raywenderlich.com/4147/core-text-tutorial-for-ios-making-a-magazine- 앱)? –

답변

2

여기 내 모든 설명 주석이 포함 된 귀속 문자열을 렌더링하는 내 우선 적용 drawRect: 메서드입니다. 이 메서드가 호출 될 때까지 UIKit은 뷰에 맞게 드로잉 환경을 적절하게 구성했으며 내용을 렌더링하는 데 필요한 드로잉 메서드와 함수를 간단하게 호출 할 수 있습니다.

/*! 
* @abstract draw the actual coretext on the context 
* 
*/ 
- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    [self.backgroundColor setFill]; 
    CGContextFillRect(context, rect); 

    if (_ctframe != NULL) CFRelease(_ctframe); 

    if (_framesetter != NULL) CFRelease(_framesetter); 

    //Creates an immutable framesetter object from an attributed string. 
    //Use here the attributed string with which to construct the framesetter object. 
    _framesetter = CTFramesetterCreateWithAttributedString((__bridge 
    CFAttributedStringRef)self.attributedString); 


    //Creates a mutable graphics path. 
    CGMutablePathRef mainPath = CGPathCreateMutable(); 

    if (!_path) { 
     CGPathAddRect(mainPath, NULL, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)); 
    } else { 
     CGPathAddPath(mainPath, NULL, _path); 
    } 

    //This call creates a frame full of glyphs in the shape of the path 
    //provided by the path parameter. The framesetter continues to fill 
    //the frame until it either runs out of text or it finds that text 
    //no longer fits. 
    CTFrameRef drawFrame = CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), 
    mainPath, NULL); 

    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
    CGContextTranslateCTM(context, 0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    // draw text 
    CTFrameDraw(drawFrame, context); 

     //clean up 
     if (drawFrame) CFRelease(drawFrame); 
    CGPathRelease(mainPath); 
    }