2010-04-21 19 views
25

में टेक्स्ट कैसे बना सकता हूं यदि UILabel में बहुत अधिक टेक्स्ट है, तो मैं अपना लेबल कैसे सेट कर सकता हूं ताकि यह फ़ॉन्ट आकार को छोटा कर सके?मैं UILabel संकीर्ण फ़ॉन्ट आकार

 descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)]; 
    [descriptionLabel setFont:[Utils getSystemFontWithSize:14]]; 
    [descriptionLabel setBackgroundColor:[UIColor clearColor]]; 
    [descriptionLabel setTextColor:[UIColor whiteColor]]; 
    descriptionLabel.numberOfLines = 1; 
    [self addSubview:descriptionLabel]; 

उत्तर

58
descriptionLabel.adjustsFontSizeToFitWidth = YES; 
descriptionLabel.minimumFontSize = 10.0; //adjust to preference obviously 

निम्न उदाहरण का परीक्षण किया और iPhone सिम्युलेटर 3.1.2 पर सत्यापित किया गया है:

UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 200, 30)]; 

descriptionLabel.font = [UIFont systemFontOfSize:14.0]; 
descriptionLabel.minimumFontSize = 10.0; 
descriptionLabel.adjustsFontSizeToFitWidth = YES; 
descriptionLabel.numberOfLines = 1; 
descriptionLabel.text = @"supercalifragilisticexpialidocious even thought he sound of it is something quite attrocious"; 
+0

मैंने उन पंक्तियों को जोड़ा है लेकिन यह काम नहीं कर रहा है। यहां तक ​​कि जब मैं अपने रेक्ट को CGRectMake (200,30,10,10) जैसे छोटे होने के लिए निर्दिष्ट करता हूं, तब भी कुछ भी नहीं होता है। –

+0

मुझे यकीन नहीं है कि आपके [उपयोग सिस्टमसिंथ विथसाइज:] वापस आ रहा है ... मैं अपना जवाब संपादित कर रहा हूं जिसमें एक उदाहरण शामिल है जिसे मैंने अभी परीक्षण और सत्यापित किया है। – prendio2

+5

आईओएस 6 के रूप में, अब आपको 'न्यूनतमफॉन्ट आकार' के बजाय 'setMinimumScaleFactor' का उपयोग करना चाहिए। –

21

एक बहु लाइन UILabel में पाठ का आकार बदलने के लिए, आप इस सहायक विधि (से code के आधार पर उपयोग कर सकते हैं 11 पिक्सेल स्टूडियो):

+ (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize { 
// use font from provided label so we don't lose color, style, etc 
UIFont *font = aLabel.font; 

// start with maxSize and keep reducing until it doesn't clip 
for(int i = maxSize; i >= minSize; i--) { 
    font = [font fontWithSize:i]; 
    CGSize constraintSize = CGSizeMake(aLabel.frame.size.width, MAXFLOAT); 

    // This step checks how tall the label would be with the desired font. 
    CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
    if(labelSize.height <= aLabel.frame.size.height) 
    break; 
} 
// Set the UILabel's font to the newly adjusted font. 
aLabel.font = font; 
} 
+0

आपके लूप के लिए, स्थिति 'i> = minSize' होना चाहिए, न कि' i> 10' –

+0

आप सही हैं .. धन्यवाद @chrispix। –

0

आप लाइनों की संख्या भी बढ़ाने के लिए यदि आवश्यक हो तो, इतनी के रूप में, स्टीव एन के समाधान का उपयोग करता है, तो बयान के साथ करना चाहते हैं:

if(labelSize.height <= aLabel.frame.size.height) 
{ 
    aLabel.numberOfLines = labelSize.height/font.lineHeight; 

    break; 
} 
+3

या बस लाइनों की संख्या 0 पर सेट करें। – Jonathan

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