मुझे लगा कि यह एक दिलचस्प सवाल था, इसलिए बनाया गया है और मुश्किल से इस परीक्षण किया ...
- (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 नामक एक संपत्ति जोड़ना।
क्या आपने लेबल को समायोजित फ़ॉन्ट आकार स्वचालित रूप से लेबल करने के लिए लेबल की 'एडजस्ट्सफॉन्टसाइजटॉफिटविड्थ' संपत्ति को YES में सेट करने पर विचार किया है? – kevboh
मुझे लगता है कि @thematerik व्यवहार के बारे में बात कर रहा है जब यह न्यूनतम फ़ॉन्ट आकार को हिट करता है तो इलिप्स के साथ प्रतिस्थापित करता है। यह एक दिलचस्प सवाल है। – danh