简体   繁体   English

根据每个细分中的标题更改分段控件的宽度?

[英]Change width of a segmented control based on titles in each segment?

Starts like this, which I like: 从这样开始,我喜欢:
Screenshot1.png

But then I add a segment and this happens: 但后来我添加了一个段,这发生了:
Screenshot2.png The width is set in IB, not in code. 宽度在IB中设置,而不是在代码中。

All I need is a method to calculate width on the fly. 我只需要一种动态计算宽度的方法。 At the end, it would do something like this: 最后,它会做这样的事情:

control.width = (labelWidths + marginWidths);
// where marginWidths = (marginWidth * control.numberOfSegments)

您可以使用属性apportionsSegmentWidthsByContent并将其设置为YES

As shown in below pic change Auto-Size property to 'Proportional to Content'. 如下图所示,将自动大小属性更改为“与内容比例”。

在此输入图像描述

The method in prgrmr's answer here works fine for its intended purpose, but this is not it. 在prgrmr的回答方法, 在这里工作正常,其预期目的,但这不是它。

Rather than adding unnecessary overhead with custom UILabel subviews, 而不是在自定义UILabel子视图中添加不必要的开销,
I've modified the example code in the above link to come up with this: 我修改了上面链接中的示例代码来得出这个:

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

The first option looks like this: 第一个选项如下所示:

segmentedControl.setWidth(100, forSegmentAt: 0)
segmentedControl.setWidth(50, forSegmentAt: 1)

That gives you individually sized segments while sticking to a value you define, which means you get to tweak the aesthetics as you want. 这样可以为您提供单独调整大小的片段,同时坚持您定义的值,这意味着您可以根据需要调整美学。 The second option looks like this: 第二个选项如下所示:

segmentedControl.apportionsSegmentWidthsByContent = true

That hands full control over to iOS, which is probably the best thing to do most of the time. 这完全可以控制iOS,这可能是大多数时候最好的事情。

Available from iOS 5.0 可从iOS 5.0获得

hackingwithswift hackingwithswift

It will adjust the size of the segmented control to accommodate the text in the text labels of each segment. 它将调整分段控件的大小以适应每个段的文本标签中的文本。

  override func viewDidLoad() {
    super.viewDidLoad()

   UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM