2011-09-04 7 views
8

CATextLayer를 사용하고 있습니다. iOS에서 맞춤 글꼴을 사용하려면 Fonts provided by application과 함께 맞춤 글꼴을 사용하는 간단한 방법이 있지만 다른 글꼴입니다. 각 문자 사이의 간격을 변경하는 방법이 있는지 궁금합니다. 나는 그렇게 할 어떤 재산도 찾지 못했습니다!CATextLayer 및 문자 사이의 추적/간격

편집 :

- (void)viewWillAppear:(BOOL)animated { 
    CTFontRef font = [self newCustomFontWithName:@"yeki" 
              ofType:@"ttf" 
             attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:16.f] 
                      forKey:(NSString *)kCTFontSizeAttribute]]; 


    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    normalTextLayer_ = [[CATextLayer alloc] init]; 
    normalTextLayer_.font = font; 
    normalTextLayer_.string = str; 
    normalTextLayer_.wrapped = YES; 
    normalTextLayer_.foregroundColor = [[UIColor purpleColor] CGColor]; 
    normalTextLayer_.fontSize = 50.f; 
    normalTextLayer_.alignmentMode = kCAAlignmentRight; 

    normalTextLayer_.frame = CGRectMake(0.f,100.f, screenBounds.size.width, screenBounds.size.height /1.f); 
    [self.view.layer addSublayer:normalTextLayer_]; 
    CFRelease(font); 
} 
+2

커닝을 의미합니까? 'CGContextSetCharacterSpacing'을 사용해 볼 수 있습니다. –

+0

네,하지만 CATextLayer 클래스의'CGContextSetCharacterSpacing'에 대한 해결책을 찾지 못했습니다! –

+0

어떤 응답 ???? –

답변

12

당신은 레이어에 NSAttributedString (또는 NSMutableAttributedString) 대신 일반 NSString를 할당하고 커닝을 조정 (Core Text String Attributes Reference 참조) kCTKernAttributeName 속성을 사용할 수 있습니다.

예 : 당신에게 증가 된 문자 간격과 헬 베티 (30) 녹색 텍스트를 제공해야

CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), 30, NULL); 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          (id)font, kCTFontAttributeName, 
          [NSNumber numberWithFloat:15.0], kCTKernAttributeName, 
          (id)[[UIColor greenColor] CGColor], kCTForegroundColorAttributeName, nil]; 
NSAttributedString *attributedString = [[[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes] autorelease]; 
CFRelease(font); 
myTextLayer.string = attributedString; 

.

+0

감사합니다. 당신이'kCTKernAttributeName'을 어떻게 사용할 수 있는지 알려주시겠습니까? 왜냐하면'NSAttributedString'으로 CATextLayer 문자열을 대체 할 때 내 텍스트는 색상, 사용자 정의 글꼴 등의 일부 효과를 느슨하게하기 때문입니다 ... –

+0

내 편집 된 질문을 보려면 내 코드를 보시기 바랍니다. : –

+0

'CATextLayer'의 색상과 글꼴 속성은 일반 문자열에만 적용됩니다. NSAttributedString은 문자열 내의 다른 범위에 적용 할 수있는 자체 속성을 정의하기 때문입니다 (다른 문자는 다른 색상을 가질 수 있습니다 ...). 'initWithString : attributes'를 사용하여 전체 문자열에 적용되는 속성 사전으로 속성 화 된 문자열을 초기화 할 수 있습니다. 사용 가능한 속성에 대한 개요는 제가 링크 된 문서를 참조하십시오. – omz