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

pro*_*rmr 10 iphone uisegmentedcontrol uifont ios

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

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

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

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

Cam*_*oft 29

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

来自iOS 5 文档:

apportionsSegmentWidthsByContent

指示控件是否尝试根据内容宽度调整分段宽度.

@property(非原子)BOOL apportionsSegmentWidthsByContent

讨论

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


pro*_*rmr 26

编辑:一个更简单的选择可能是使用apportionsSegmentWidthsByContent@Camsoft的答案中提到的iOS 5属性


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


Boo*_*Boy 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]];
        }
    }           
}
Run Code Online (Sandbox Code Playgroud)