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;
preguntaba sobre eso también .. – Nils