prgrmr के जवाब में विधि here अपने इच्छित उद्देश्य के लिए ठीक काम करता है, लेकिन यह ऐसा नहीं है।
बजाय कस्टम UILabel subviews के साथ अनावश्यक भूमि के ऊपर जोड़ने,
मैं उदाहरण कोड ऊपर के लिंक में इस के साथ आने के लिए संशोधित किया है:
- (void)resizeSegmentsToFitTitles:(UISegmentedControl *)control {
CGFloat textWidth = 0; // total width of all text labels
CGFloat marginWidth = 0; // total width of all margins
NSUInteger nSegments = control.subviews.count;
UIView *aSegment = [control.subviews objectAtIndex:0];
UIFont *theFont = nil;
// get font for segment title label
for (UILabel *label in aSegment.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
theFont = label.font;
break;
}
}
// calculate width of text in each segment
for (NSUInteger i = 0; i < nSegments; i++) {
NSString *title = [control titleForSegmentAtIndex:i];
CGFloat width = [title sizeWithFont:theFont].width;
CGFloat margin = 15;
if (width > 200) {
NSString *ellipsis = @"…";
CGFloat width2 = [ellipsis sizeWithFont:theFont].width;
while (width > 200-width2) {
title = [title substringToIndex:title.length-1];
width = [title sizeWithFont:theFont].width;
}
title = [title stringByAppendingString:ellipsis];
}
[control setTitle:title forSegmentAtIndex:i];
textWidth += width;
marginWidth += margin;
}
// resize segments to accomodate text size, evenly split total margin width
for (NSUInteger i = 0; i < nSegments; i++) {
// size for label width plus an equal share of the space
CGFloat textWidth = [[control titleForSegmentAtIndex:i]
sizeWithFont:theFont].width;
// the control leaves a 1 pixel gap between segments if width
// is not an integer value; roundf() fixes this
CGFloat segWidth = roundf(textWidth + (marginWidth/nSegments));
[control setWidth:segWidth forSegmentAtIndex:i];
}
// set control width
[control setFrame:CGRectMake(0, 0, (textWidth + marginWidth), 30)];
}
मैं हिस्सों में बंटा हुआ नियंत्रक खिताब छोटा जब यह हालांकि अंतिम खंड नहीं tapable नहीं है चाहिए ... किसी भी ideea क्यों के साथ एक समस्या को दूर करने के iOS7 में यह उपयोग कर रहा हूँ? – user1028028
http://stackoverflow.com/questions/18890428/uisegmentedcontrol-truncates-segment-titles – user1028028
[यहां] (http://stackoverflow.com/a/23492688/2254935) सरलीकृत और आईओएस 7 अनुकूल संस्करण है। –