2012-05-01 5 views
5

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

धन्यवाद!

+0

क्या आपने लेबल को समायोजित फ़ॉन्ट आकार स्वचालित रूप से लेबल करने के लिए लेबल की 'एडजस्ट्सफॉन्टसाइजटॉफिटविड्थ' संपत्ति को YES में सेट करने पर विचार किया है? – kevboh

+0

मुझे लगता है कि @thematerik व्यवहार के बारे में बात कर रहा है जब यह न्यूनतम फ़ॉन्ट आकार को हिट करता है तो इलिप्स के साथ प्रतिस्थापित करता है। यह एक दिलचस्प सवाल है। – danh

उत्तर

1

दुर्भाग्य से, ऐसा लगता है कि इस तरह के एक विकल्प iOS पर शामिल नहीं है, यह समान प्रश्न के अनुसार: How to change truncate characters in UILabel?

हालांकि, ऊपर उल्लिखित सवाल राज्य में जवाब के रूप में, यह आसानी से अपने आप को किया जा सकता है। आपको बस इतना करना है कि स्ट्रिंग को छोटा कर दिया गया है और आपके चुने हुए अंतराल चरित्रों के लिए आवश्यक राशि घटाएं। फिर शेष को एक अलग स्ट्रिंग में रखें।

इस विधि के लिए, इस सवाल का जवाब भी उपयोगी होगा:

रूप Javanator कहा कि तुम अपनी खुद की काट-छांट करने के लिए होगा। आप आकार का उपयोग करते हैं WithFont: forWidth: lineBreakMode: किसी निश्चित फ़ॉन्ट के साथ स्ट्रिंग की चौड़ाई प्राप्त करने के लिए एनएसएसटींग कक्षा में UIKit परिवर्धन पर संदेश। यह सभी प्रकार के फ़ॉन्ट्स को संभालेगा।

1

मुझे लगा कि यह एक दिलचस्प सवाल था, इसलिए बनाया गया है और मुश्किल से इस परीक्षण किया ...

- (void)setText:(UILabel *)label withText:(NSString *)text andTruncationSuffix:(NSString *)truncationSuffix { 

    // just set the text if it fits using the minimum font 
    // 
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]]; 
    if (size.width <= label.bounds.size.width) { 
     label.text = text; 
     return; 
    } 

    // build a truncated version of the text (using the custom truncation text) 
    // and shrink the truncated text until it fits 
    NSInteger lastIndex = text.length; 
    CGFloat width = MAXFLOAT; 

    NSString *subtext, *ellipticalText; 

    while (lastIndex > 0 && width > label.bounds.size.width) { 
     subtext = [text substringToIndex:lastIndex]; 
     ellipticalText = [subtext stringByAppendingString:truncationSuffix]; 
     width = [ellipticalText sizeWithFont:[UIFont systemFontOfSize:label.minimumFontSize]].width; 
     lastIndex--; 
    } 
    label.text = ellipticalText; 
} 

इसे इस तरह कॉल करें:

[self setText:self.label withText:@"Now is the time for all good men to come to the aid of their country" andTruncationSuffix:@" more"]; 

यह आपके लिए काम करता है, तो आप कर सकते थे SetText: विधि को ओवरराइड करने के लिए इसका उपयोग करके UILabel का उप-वर्ग जोड़ने पर विचार करें, और truncatedSuffix नामक एक संपत्ति जोड़ना।

+0

उस समय पाठ सेट किया गया था, लेबल की चौड़ाई निर्धारित नहीं की जा सकती है, खासकर यदि आप Autolayout का उपयोग कर रहे हैं। – ozgur

+0

@ozgur - हाँ, यह पोस्ट अब बहुत पुराना है, ऑटो लेआउट और सभी नई टाइपोग्राफी सामग्री दी गई है। यहां तक ​​कि 'sizeWithFont' अब भी बहिष्कृत है। शायद मैं इसे जल्द ही कुछ बिंदु पर फिर से लिखूंगा। – danh

1

यदि आप आईओएस संस्करण> 8.0 का उपयोग कर रहे हैं, तो आप ResponsiveLabel का उपयोग कर सकते हैं। यहां आप कस्टमाइज्ड ट्रंकेशन टोकन प्रदान कर सकते हैं और इसे क्रियान्वित करने के लिए क्रियाओं को परिभाषित कर सकते हैं।

NSString *expansionToken = @"Read More ..."; 
NSString *str = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; 
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:kExpansionToken attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:self.customLabel.font}]; 
[self.customLabel setAttributedTruncationToken:attribString withAction:^(NSString *tappedString) { 
NSLog(@"Tap on truncation text"); 
}]; 
[self.customLabel setText:str withTruncation:YES]; 
संबंधित मुद्दे