2012-06-15 14 views
5

Ho un CATextlayer di una determinata dimensione e il testo NSAttributedString di lunghezza sconosciuta.Catextlayer modifica la dimensione di FONT per adattarsi al frame

devo regolare il carattere-dimensione in modo che il testo possa telaio (non viceversa :)

Delle idee dove iniziare? :)

[Modifica] come nall indica, posso determinare la lunghezza della stringa, ovviamente, è un testo inserito dall'utente che devo inserire in una scatola di dimensioni fisse.

+0

Quando si dice 'il testo di una lunghezza sconosciuta', si fa a non significa noto al momento della compilazione? Ad un certo punto, * devi * conoscere la lunghezza ... – nall

+0

Haha - ok, buon punto. È un testo che è stato inserito dall'utente ad un certo punto. Certo, posso ottenere la lunghezza della corda ... :) – Swissdude

risposta

1

ho finito per fare questo:

textlayer è un CATextlayer

theString è un NSMutableAttributedString

E sì, non è molto elegante e potrebbe sicuramente essere migliorata;)

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

Che cosa significa "def"? –

0

Credo che tu stia chiedendo la stessa cosa di this question.

Sembra che sia necessario eseguire un'iterazione su una varietà di dimensioni dei caratteri per determinare quale si adatta al tuo rect.

+1

Che suona ingombrante ... – Swissdude

+0

"pagina non trovata" all'interno della risposta url –

6

L'ho raggiunto in questo modo:

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

è "MaximunWidth" il frame CALayerWidth? –

+0

@OfirMalachi No, è la dimensione fissa che non può essere superata dalla dimensione del testo –

-1
CATextLayer *textLayer; 

[textLayer setWrapped: TRUE]; 

Questo, si spera, funzionerà

+0

Wow - questo sembra MOLTO corto ...;) Farò un tentativo! Grazie! – Swissdude

Problemi correlati