2011-09-04 21 views
8

Estoy usando CATextLayer, para usar una fuente personalizada en iOS, sé que hay una forma simple de usar fuentes personalizadas con Fonts provided by application pero esta es una fuente diferente. Me preguntaba si hay alguna forma de cambiar el espacio entre cada personaje. ¡No encontré ninguna propiedad para hacerlo!CATextLayer y tracking/spacing between characters

Editado:

- (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

¿Te refieres al kerning? Puedes intentar usar 'CGContextSetCharacterSpacing'. –

+0

sí, pero no encontré ninguna solución con 'CGContextSetCharacterSpacing' para la clase CATextLayer! –

+0

cualquier respuesta ???? –

Respuesta

12

Se puede asignar un NSAttributedString (o NSMutableAttributedString) en lugar de una llanura NSString a la capa y utilizar el atributo kCTKernAttributeName (ver Core Text String Attributes Reference) para ajustar el espaciado manual.

Ejemplo:

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; 

que debe darle el texto en verde en Helvetica 30 con un mayor espacio entre caracteres.

+0

gracias. y por favor díganos cómo puedo usar esto: 'kCTKernAttributeName' porque cuando reemplazo mi cadena CATextLayer con' NSAttributedString' mi texto pierde algunos efectos como colores, fuentes personalizadas y etc ... –

+0

Por favor vea mi pregunta editada para ver mi código : –

+0

Los atributos de color y fuente de 'CATextLayer' solo se aplican a strings simples porque un' NSAttributedString' define sus propios atributos que pueden aplicarse a diferentes rangos dentro de la cadena (diferentes caracteres pueden tener diferentes colores ...). Puede usar 'initWithString: attributes' para inicializar la cadena atribuida con un diccionario de atributos que se aplica a toda la cadena. Consulte la documentación a la que me he vinculado para obtener una descripción general de los atributos disponibles. – omz

Cuestiones relacionadas