2011-11-21 11 views
6

Si configuro textLayer.wrapped = YES, ¿cómo cambio el tamaño de textLayer para contener el texto ajustado? Es decir, ¿cómo obtengo la nueva altura del textLayer?CATextLayer wrapped sizeToFit?

Básicamente, quiero algo como -[UILabel sizeToFit].

+0

preguntaba sobre eso también .. – Nils

Respuesta

2

Lo primero que debe hacer es obtener el tamaño del texto.

Afortunadamente, el NSString UIKit Additions Reference ofrece un método para hacer exactamente lo siguiente:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode 

que le dará un CGSize que entonces se puede utilizar para establecer el marco de su UILabel o lo que sea subclase de UIView que eres utilizando.

Así, suponiendo textLayer es un UILabel - en lugar de un CALayer - que va a terminar con algo como esto:

UIFont *myFont = [UIFont boldSystemFontOfSize:12.0f]; 
CGSize myFontSize = [myString sizeWithFont:myFont]; 
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myFontSize.width, myFontSize.height)]; 
myLabel.text = newTitle; 
myLabel.font = myFont; 
+1

La pregunta es para CALayer. –

Cuestiones relacionadas