2012-04-09 1 views
3

나는 사용자 정의 라인 간격 텍스트를 갖고 싶어, 그래서 CTParagraphStyleAttributte와 속성 문자열을 쓴에 전달 내 CATextLayer :CATextLayer + NSAttributtedString + CTParagraphStyleRef

UIFont *font = [UIFont systemFontOfSize:20]; 
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)font.fontName, 
             font.pointSize, NULL); 
CGColorRef cgColor = [UIColor whiteColor].CGColor; 
CGFloat leading = 25.0; 
CTTextAlignment alignment = kCTRightTextAlignment; // just for test purposes 
const CTParagraphStyleSetting styleSettings[] = { 
    {kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof(CGFloat), &leading}, 
    {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment} 
}; 
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(styleSettings, 2)); 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          (id)ctFont, (id)kCTFontAttributeName, 
          (id)cgColor, (id)kCTForegroundColorAttributeName, 
          (id)paragraphStyle, (id)kCTParagraphStyleAttributeName, 
          nil]; 
CFRelease(ctFont); 
CFRelease(paragraphStyle); 

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] 
               initWithString:string 
                attributes:attributes]; 
_textLayer.string = attrStr; 
[attrStr release]; 

그러나 행 높이가 변경되지 . 나는 여기서 뭔가를 놓치고 있다고 생각하지만, 나는 무엇을 모른다.

나는 kCTParagraphStyleSpecifierLineSpacingAdjustmentkCTParagraphStyleSpecifierLineSpacing으로 시도했지만 그 중 하나가 작동하지 않는 것 (?). 나는 또한 을 사용하여 정렬을 설정하려고 시도했다. (CATextLayer은 그 속성을 가지고있다.) 단지 kCTParagraphStyleAttributeName을 테스트하는 것은 실제로 효과가있다.

나 자신에게 물어볼 몇 가지 미친 값 (예 : CTParagraphStyleCreate(styleSettings, -555);)을 전달하더라도 알 수 있습니다. CATextLayer은 단락 속성을 지원합니까? 그렇다면, 내가 여기서 무엇을 놓치고 있습니까?

+0

가) 인수 arg2는 실제로 CFIndex로 입력해야합니다. 하지만 이것은 확장을 위해 특별히 정의 된 정수형이기 때문에 실제로 문제가되는 것은 아닌지 의심 스럽습니다. B) 나는 paragraphStyle을 통해 탭 중지를 받기 위해 CATextLayer를 얻을 수 있었으므로 일부 속성을 지원해야합니다. 그러나 첫 번째 줄과 머리 들여 쓰기는 효과가 없었습니다. 적어도 iOS 4.3에는 없었습니다. (누군가가 답을 갖고 있기를 바랍니다. 왜냐하면 leading는 textLayer 기능에 중요해 보입니다.) – Wienke

답변

3

NSAttributedString을 CATextLayer에 넣고 코드를 시험해 보았는데 말했듯이 형식이 무시되었습니다.

그런 다음 CTFrameDraw을 사용하여 UIView drawRect 메서드에 정확히 동일한 속성의 문자열을 그리려고했는데 모든 서식이 적용됩니다. CATextLayer가 대부분의 서식을 무시한다고 가정 할 수 있습니다. CATextLayer Class Reference에는 효율성을 위해 무엇을하는지에 대한 여러 가지 경고가 있습니다.

UIView이 아닌 CALayer으로 그리려면 실제로 사용자 CALayer 하위 클래스를 만들거나 위임하여 드로잉을 수행 할 수 있습니다.

- (void)drawRect:(CGRect)rect 
{ 
    // 
    // Build attrStr as before. 
    // 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGRect bounds = [self bounds]; 

    // Text ends up drawn inverted, so we have to reverse it. 
    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity); 
    CGContextTranslateCTM(ctx, bounds.origin.x, bounds.origin.y+bounds.size.height); 
    CGContextScaleCTM(ctx, 1, -1); 

    // Build a rectangle for drawing in. 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, bounds); 

    // Create the frame and draw it into the graphics context 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) attrStr); 
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 
    CFRelease(framesetter); 
    CFRelease(path); 

    // Finally do the drawing. 
    CTFrameDraw(frame, ctx); 
    CFRelease(frame);   
} 
+0

Thanks Dondragmer;) 이것은 CATextLayer가 NSAttributedString의 모든 속성을 지원하지 않는다는 증거입니다. – nacho4d