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