2010-11-03 12 views
7

Sto tentando di trovare la dimensione massima del carattere che si adatta a un dato retto per una determinata stringa. L'obiettivo dell'algoritmo è riempire il più possibile il più possibile con un font il più grande possibile. Il mio approccio - che è stato modificato da quello che ho trovato online - fa un buon lavoro, ma spesso non riempie l'intero rect. Mi piacerebbe vedere un po 'di collaborazione su come migliorare questo algoritmo in modo che ognuno possa trarre beneficio da esso:Calcola la dimensione massima del carattere che si adatta a un rect?

-(float) maxFontSizeThatFitsForString:(NSString*)_string 
           inRect:(CGRect)rect 
          withFont:(NSString *)fontName 
          onDevice:(int)device 
{ 

    // this is the maximum size font that will fit on the device 
    float _fontSize = maxFontSize; 
    float widthTweak; 

    // how much to change the font each iteration. smaller 
    // numbers will come closer to an exact match at the 
    // expense of increasing the number of iterations. 
    float fontDelta = 2.0; 

    // sometimes sizeWithFont will break up a word 
    // if the tweak is not applied. also note that 
    // this should probably take into account the 
    // font being used -- some fonts work better 
    // than others using sizeWithFont. 
    if(device == IPAD) 
     widthTweak = 0.2; 
    else 
     widthTweak = 0.2; 

    CGSize tallerSize = 
      CGSizeMake(rect.size.width-(rect.size.width*widthTweak), 100000); 
    CGSize stringSize = 
      [_string sizeWithFont:[UIFont fontWithName:fontName size:_fontSize] 
       constrainedToSize:tallerSize]; 

    while (stringSize.height >= rect.size.height) 
    {  
     _fontSize -= fontDelta; 
     stringSize = [_string sizeWithFont:[UIFont fontWithName:fontName 
              size:_fontSize] 
          constrainedToSize:tallerSize]; 
    } 

    return _fontSize; 
} 
+0

Quando si dice "riempi l'intero retto" si intende solo sull'orizzontale? – Magnus

risposta

4

utilizzare il seguente metodo per calcolare il tipo di carattere, che può andare bene, per un dato rect e stringa.

È possibile modificare il carattere su quello desiderato. Inoltre, se richiesto è possibile aggiungere un'altezza font predefinita;

Il metodo è auto esplicativo.

-(UIFont*) getFontTofitInRect:(CGRect) rect forText:(NSString*) text { 
     CGFloat baseFont=0; 
     UIFont *myFont=[UIFont systemFontOfSize:baseFont]; 
     CGSize fSize=[text sizeWithFont:myFont]; 
     CGFloat step=0.1f; 

     BOOL stop=NO; 
     CGFloat previousH; 
     while (!stop) { 
      myFont=[UIFont systemFontOfSize:baseFont+step ]; 
      fSize=[text sizeWithFont:myFont constrainedToSize:rect.size lineBreakMode:UILineBreakModeWordWrap]; 

      if(fSize.height+myFont.lineHeight>rect.size.height){   
       myFont=[UIFont systemFontOfSize:previousH]; 
       fSize=CGSizeMake(fSize.width, previousH); 
       stop=YES; 
      }else { 
       previousH=baseFont+step; 
      } 

      step++; 
    } 
    return myFont; 

} 
0

Non c'è bisogno di perdere tempo a fare dei loop. Innanzitutto, misurare la larghezza e l'altezza del testo alle impostazioni del punto carattere massimo e minimo. A seconda se più restrittivo, larghezza o altezza, utilizzare il seguente calcolo:

Se la larghezza è più restrittivo (cioè, maxPointWidth/rectWidth > maxPointHeight/rectHeight) utilizzare:

pointSize = minPointSize + rectWidth * [(maxPointSize - minPointSize)/(maxPointWidth - minPointWidth)] 

Altrimenti, se l'altezza è uso più restrittivo:

pointSize = minPointSize + rectHeight * [(maxPointSize - minPointSize)/(maxPointHeight - minPointHeight)] 
0

Potrebbe essere impossibile riempire completamente un rettangolo.

Supponiamo che ad una certa dimensione del carattere ci siano due righe di testo, che riempiono lo schermo orizzontalmente, ma in verticale ci sono quasi tre linee di spazio.

Se si aumenta leggermente la dimensione del carattere, le linee non si adattano più, quindi sono necessarie tre linee, ma tre linee non si adattano verticalmente.

Quindi non hai altra scelta che vivere con il divario verticale.

Problemi correlati