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

时间:2011-07-14 04:27:41

标签: iphone ios uisegmentedcontrol uifont

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

http://morrisphotoart.com/tmp/Screenshot2011-07-13_21.17.33.png

如果我知道哪种字体并调用setWidth:forSegmentAtIndex:方法,我可以编写代码来计算段宽,但是如何获取字体呢?或者还有另一种方式吗?

左侧和中间段的标题不固定,因此我无法对宽度进行硬编码。

4 个答案:

答案 0 :(得分:28)

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

来自iOS 5 docs

  

apportionsSegmentWidthsByContent

     

表示是否控制   尝试根据内容宽度调整分段宽度。

     

@property(非原子)BOOL apportionsSegmentWidthsByContent

     

<强>讨论

     

如果此属性的值为YES,则表示宽度值为的段   为0时,控件尝试根据它们调整分段宽度   内容宽度。

答案 1 :(得分:26)

编辑:更简单的选择可能是使用@Camsoft answer here

中提到的iOS 5属性apportionsSegmentWidthsByContent

好吧,我最终通过子视图(可能会在未来的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];
    }
}

答案 2 :(得分:8)

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]];
        }
    }           
}

答案 3 :(得分:-1)

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);
}