2010-05-25 13 views

risposta

19

Ho fatto qualcosa del genere in passato.

-(void)calcFontSizeToFitRect:(NSRect)r { 
    float targetWidth = r.size.width - xMargin; 
    float targetHeight = r.size.height - yMargin; 

    // the strategy is to start with a small font size and go larger until I'm larger than one of the target sizes 
    int i; 
    for (i=minFontSize; i<maxFontSize; i++) { 
     NSDictionary* attrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:currentFontName size:i], NSFontAttributeName, nil]; 
     NSSize strSize = [string sizeWithAttributes:attrs]; 
     [attrs release]; 
     if (strSize.width > targetWidth || strSize.height > targetHeight) break; 
    } 
    [self setCurrentFontSize:(i-1)]; 
} 

La variabile di stringa è il testo che si desidera ridimensionare. Le variabili xMargin e yMargin sono per la spaziatura che vuoi. Le variabili minFontSize e maxFontSize danno dei limiti a quanto piccolo o grande vuoi andare.

+0

wow. Fantastico! Grazie mille!!!!!! –

+0

Ho appena trovato questo e mi ha aiutato un sacco, grazie! –

+1

Questo non funziona molto bene per i campi di testo multilinea. –