我如何计算UISegmentedControl段的正确宽度?

我正在尝试使用UISegmentedControl,但无法计算段的宽度。 控件使得所有段的宽度都相同,但对于某些标题不起作用,如下所示:

http://morrisphotoart.com/tmp/Scr​​eenshot2011-07-13_21.17.33.png

我可以编写代码来计算段的宽度,如果我知道哪种字体,并调用setWidth:forSegmentAtIndex:方法,但我怎样才能得到字体? 还是有另一种方式?

左侧和中间段的标题不固定,所以我不能硬编码的宽度。

如果您可以支持iOS 5及更高版本,则可以使用属性apportionsSegmentWidthsByContent并将其设置为YES

从iOS 5 文档 :

apportionsSegmentWidthsByContent

指示控件是否尝试根据其内容宽度来调整段宽。

@property(nonatomic)BOOL apportionsSegmentWidthsByContent

讨论

如果此属性的值为YES,则对于宽度值为0的段,控件将尝试根据其内容宽度来调整段宽。

编辑:更容易的select可能是使用iOS 5财产apportionsSegmentWidthsByContent在@ Camsoft的答案中提到这里


那么,我最终通过子视图(可能在未来的iOS中打破)获得UIFont。 作为一个模块化/重用aficianado,我写了这个例程来做到这一点,然后分配空间,使控件中的片段均匀分布的标题。

 -(void)resizeSegmentsToFitTitles:(UISegmentedControl*)segCtrl { CGFloat totalWidths = 0; // total of all label text widths NSUInteger nSegments = segCtrl.subviews.count; UIView* aSegment = [segCtrl.subviews objectAtIndex:0]; UIFont* theFont = nil; for (UILabel* aLabel in aSegment.subviews) { if ([aLabel isKindOfClass:[UILabel class]]) { theFont = aLabel.font; break; } } // calculate width that all the title text takes up for (NSUInteger i=0; i < nSegments; i++) { CGFloat textWidth = [[segCtrl titleForSegmentAtIndex:i] sizeWithFont:theFont].width; totalWidths += textWidth; } // width not used up by text, its the space between labels CGFloat spaceWidth = segCtrl.bounds.size.width - totalWidths; // now resize the segments to accomodate text size plus // give them each an equal part of the leftover space for (NSUInteger i=0; i < nSegments; i++) { // size for label width plus an equal share of the space CGFloat textWidth = [[segCtrl titleForSegmentAtIndex:i] sizeWithFont:theFont].width; // roundf?? the control leaves 1 pixel gap between segments if width // is not an integer value, the roundf fixes this CGFloat segWidth = roundf(textWidth + (spaceWidth / nSegments)); [segCtrl setWidth:segWidth forSegmentAtIndex:i]; } } 
 NSArray *itemArray = [NSArray arrayWithObjects: @"Title1", @"Title2", @"Titl3", @"Title4",nil]; segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; segmentedControl.frame = CGRectMake(0, 0, 310, 35); segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; segmentedControl.selectedSegmentIndex = 0; [segmentedControl addTarget:self action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged]; segmentedControl.tintColor=[UIColor grayColor]; for (id segment in [segmentedControl subviews]) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { [label setTextAlignment:UITextAlignmentCenter]; [label setFont:[UIFont boldSystemFontOfSize:12]]; } } } 

iOS 7.1.1友好。 我们只计算UISegmentedControl新的框架大小,其余的(分段比例)将由iOS解决(基于apportionsSegmentWidthsByContent)。

请注意,如果“自动布局”处于打开状态,则此操作不起作用。

 void styleSegment_fitTitles(UISegmentedControl *segCtrl) { // Fit segments' titles [segCtrl sizeToFit]; CGFloat em = font.pointSize; CGRect segCtrlRect = segCtrl.bounds; CGFloat segCtrlWidth = segCtrlRect.size.width + segCtrl.numberOfSegments*em; CGFloat segCtrlHeight = segCtrlRect.size.height + em; CGFloat segmentY = segCtrl.frame.origin.y - (segCtrlHeight - segCtrlRect.size.height)/2; segCtrl.frame = CGRectMake(segCtrl.frame.origin.x, segmentY, segCtrlWidth, segCtrlHeight); }