Ios 使用多个标签滚动视图

Ios 使用多个标签滚动视图,ios,swift,Ios,Swift,我已经创建了一个滚动视图 我正在使用故事板,但当我将这些标签添加到我的ViewController并为它们设置正确的约束时,它在iPhone 5s上看起来很好,但在iPhone 6s上看起来很奇怪,因为最后一个标签下面会有太多空间 因此,当我在ViewController中设置视图的高度时,我应该如何在更大的设备上消除这些空间 我的约束可能有问题 我对iphone5s的看法: 我对iphone6s的看法: 您可以使用此函数计算文本渲染大小: //ARC code //font: render

我已经创建了一个滚动视图

我正在使用故事板,但当我将这些标签添加到我的ViewController并为它们设置正确的约束时,它在iPhone 5s上看起来很好,但在iPhone 6s上看起来很奇怪,因为最后一个标签下面会有太多空间

因此,当我在ViewController中设置视图的高度时,我应该如何在更大的设备上消除这些空间

我的约束可能有问题

我对iphone5s的看法:

我对iphone6s的看法:


您可以使用此函数计算文本渲染大小:

//ARC code
//font: render font
//renderMaxWidth: max width of a line
//lineBreakMode: line break mode
- (CGSize) calculateLabelRenderSizeWith:(UIFont *) font renderMaxWidth:(CGFloat) width lineBreakMode:(NSLineBreakMode) mode 
{
    CGSize availableSize = CGSizeZero;
    availableSize.width = width;
    availableSize.height = MAXFLOAT;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = mode;

    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    NSDictionary *attributes = @{
                             NSFontAttributeName: font,
                             NSParagraphStyleAttributeName: style
                             };
    CGSize textNodeSize = [self boundingRectWithSize:availableSize
                                         options:options
                                      attributes:attributes
                                         context:nil].size;
    return textNodeSize;
}
斯威夫特:

func __calculateLabelRenderSizeWith(font: UIFont, renderMaxWidth: CGFloat, lineBreakMode:NSLineBreakMode) -> CGSize
{
    var availableSize = CGSizeZero
    availableSize.width = renderMaxWidth
    availableSize.height = CGFloat.max

    let style = NSMutableParagraphStyle()
    style.lineBreakMode = lineBreakMode

    let attributes = [
        NSFontAttributeName: font,
        NSParagraphStyleAttributeName: style
    ]
    let textNodeSize = boundingRectWithSize(availableSize, options: .UsesLineFragmentOrigin, attributes: attributes, context: nil).size
    return textNodeSize;
}

您需要使用autolayout根据屏幕高度调整uiscrollview的内容大小根据您的约束和外观添加屏幕我为每个标签添加了顶部、前导和尾随约束