2010-11-03 8 views
7

मैं अधिकतम फ़ॉन्ट आकार ढूंढने का प्रयास कर रहा हूं जो किसी दिए गए स्ट्रिंग के लिए दिए गए रेक्ट में फिट होगा। एल्गोरिदम का लक्ष्य जितना संभव हो सके उतना बड़ा जितना संभव हो सके उतना बड़ा फ़ॉन्ट भरना है। मेरा दृष्टिकोण - जिसे मैंने ऑनलाइन पाया है उससे संशोधित किया गया है - एक उचित काम करता है, लेकिन यह अक्सर पूरे आय को भरता नहीं है। मैं कैसे इस एल्गोरिथ्म सुधारने के लिए कुछ सहयोग को देखने के लिए इतना है कि हर कोई यह से लाभ हो सकता खुशी होगी:अधिकतम फ़ॉन्ट आकार की गणना करें जो एक रेक्ट में फिट बैठती है?

-(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

जब आप कहते हैं कि "संपूर्ण आय भरें" तो आप केवल क्षैतिज पर ही मतलब रखते हैं? – Magnus

उत्तर

4

फ़ॉन्ट जो, फिट कर सकते हैं किसी दिए गए रेक्ट और स्ट्रिंग के लिए गणना करने के लिए निम्न विधि का उपयोग।

आप जिस फ़ॉन्ट की आवश्यकता है उसे फ़ॉन्ट बदल सकते हैं। इसके अलावा, यदि आवश्यक हो तो आप एक डिफ़ॉल्ट फ़ॉन्ट ऊंचाई जोड़ सकते हैं;

विधि स्वयं स्पष्टीकरणपूर्ण है।

-(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

लूप करने में समय बर्बाद करने की कोई आवश्यकता नहीं है। सबसे पहले, अधिकतम और न्यूनतम फ़ॉन्ट बिंदु सेटिंग्स पर टेक्स्ट चौड़ाई और ऊंचाई को मापें। पर जो भी, अधिक प्रतिबंधात्मक चौड़ाई या ऊंचाई है आधार पर निम्न गणित का उपयोग करें:

तो चौड़ाई अधिक प्रतिबंधात्मक है (यानी, maxPointWidth/rectWidth > maxPointHeight/rectHeight) का उपयोग करें:

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

वरना, ऊंचाई अधिक प्रतिबंधात्मक उपयोग है यदि:

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

आयत को पूरी तरह से भरना असंभव हो सकता है।

एक निश्चित फ़ॉन्ट आकार पर कहें कि आपके पास टेक्स्ट की दो पंक्तियां हैं, दोनों स्क्रीन क्षैतिज रूप से भर रही हैं, लेकिन लंबवत रूप से आपके पास लगभग तीन पंक्तियां हैं।

यदि आप फ़ॉन्ट आकार को केवल एक छोटा सा बढ़ाते हैं, तो लाइनें अब फिट नहीं होती हैं, इसलिए आपको तीन लाइनों की आवश्यकता होती है, लेकिन तीन पंक्तियां लंबवत रूप से फिट नहीं होती हैं।

तो आपके पास लंबवत अंतर के साथ रहने के अलावा कोई विकल्प नहीं है।

संबंधित मुद्दे

 संबंधित मुद्दे