2012-06-15 17 views
5

Tengo un CATextlayer de un cierto tamaño y NSAttributedString texto de longitud desconocida.CATextlayer cambia el tamaño de la FUENTE para que se ajuste al marco

tengo que ajustar el tamaño de fuente para que el texto se ajuste al marco (no al revés :)

¿Alguna idea de por dónde empezar? :)

[Editar] como todo indica, puedo determinar la longitud de la cadena, por supuesto, es un texto introducido por el usuario que tengo que caber en una caja de tamaño fijo.

+0

Cuando se dice 'texto de una longitud desconocida', es lo que no significa conocido en tiempo de compilación? En algún punto, * debes * saber la longitud ... – nall

+0

Jaja - vale, buen punto. Es texto que ha sido ingresado por el usuario en algún momento. Por supuesto, puedo obtener la longitud de la cuerda ... :) – Swissdude

Respuesta

1

que terminé haciendo esto:

textlayer es una CATextlayer

theString es una NSMutableAttributedString

Y sí, no es muy elegante y definitivamente podría mejorarse;)

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)theString); 

    CGRect columnRect = CGRectMake(0, 0 , 320, 150); 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddRect(path, NULL, columnRect); 

    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 

    CFRange frameRange = CTFrameGetVisibleStringRange(frame); 

    int fontSize = 18; 

    while(theString.string.length > frameRange.length){ 

     fontSize--; 

     CFStringRef fontName = (__bridge CFStringRef)[defs objectForKey:@"font"]; 

     CTFontRef font = CTFontCreateWithName(fontName, fontSize, NULL); 

     [theString addAttribute:(NSString *)kCTFontAttributeName 
          value:(__bridge id)font 
          range:NSMakeRange(0, theString.string.length)]; 

     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)theString); 

     CGRect columnRect = CGRectMake(0, 0 , 320, 150); 

     CGMutablePathRef path = CGPathCreateMutable(); 
     CGPathAddRect(path, NULL, columnRect); 

     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 

     frameRange = CTFrameGetVisibleStringRange(frame); 

     textLayer.string = theString; 
    } 
+0

¿Qué significa "defs"? –

0

Creo que está preguntando lo mismo que this question.

Parece que tiene que iterar sobre una variedad de tamaños de fuente para determinar cuál se ajustará dentro de su rect.

+1

Eso suena engorroso ... – Swissdude

+0

"página no encontrada" dentro de tu url de respuesta –

6

Lo logré haciendo esto:

float fontSize = InitialFontSize; 
    UIFont *myFont = [UIFont boldSystemFontOfSize:fontSize]; 
    CGSize myFontSize = [YourTextHere sizeWithFont:myFont]; 
    while (myFontSize.width >= MaximunWidth) { 
     fontSize -= 0.1f; 
     myFont = [UIFont boldSystemFontOfSize:fontSize]; 
     myFontSize = [YourTextHere sizeWithFont:myFont]; 
    } 
    CATextLayer *textLayer = [CATextLayer layer]; 
    [textLayer setFrame:CGRectMake(MaximunWidth - myFontSize.width/2, MaximunHeight - myFontSize.height/2, myFontSize.width, myFontSize.height)]; 
    [textLayer setFontSize:fontSize]; 
    [textLayer setString:YourTextHere]; 

    [textLayer setAlignmentMode:kCAAlignmentCenter]; 
+0

es "MaximunWidth" el marco CALayerWidth? –

+0

@OfirMalachi No, es el tamaño fijo que no puede ser excedido por el tamaño del texto –

-1
CATextLayer *textLayer; 

[textLayer setWrapped: TRUE]; 

Se espera que esto funcionará

+0

Wow - esto se ve MUY corto ...;) ¡Lo intentaré! ¡Gracias! – Swissdude

Cuestiones relacionadas