简体   繁体   English

使用Interface Builder动态调整UILabel的大小并在其下方重新定位UIViews

[英]Resize UILabel dynamically and reposition UIViews below it using Interface Builder

I have a UILabel that can contain any number of lines with a constrained width. 我有一个UILabel ,可以包含任意数量的宽度受限制的行。 I have no problem resizing a UILabel to fit the given text, however, I am implementing a 'View More...'/'View Less...' button, which will either increase or decrease the size of the UILabel . 我可以毫无问题地调整UILabel大小以适合给定的文本,但是,我正在实现一个“ View More ...” /“ View Less ...”按钮,该按钮将增大或减小UILabel的大小。 Based on this, any UIView's below this UILabel will be affected, and will need to be repositioned on their y-axis accordingly. 基于此,此UILabel下方的所有UIView's都会受到影响,并且需要相应地在其y轴上重新放置。

I am trying to use the built-in features within the XIB files and am having no luck at all. 我试图使用XIB文件中的内置功能,但一点都不走运。

Autosizing settings for the UILabel: 自动调整UILabel的设置:

自动调整UILabel

Autosizing for all UIViews below the UILabel: 自动调整UILabel下所有UIView的大小:

在此处输入图片说明

Would anyone be able to point me in the right direction here? 有人能在这里向我指出正确的方向吗? So essentially a scenario would be: 因此,从本质上讲,方案是:

  1. Screen loads 屏幕负荷
  2. Label is populated with a certain amount of the available text with constrained width 标签中填充了一定数量的宽度受限制的可用文本
  3. UIView's below UILabel are repositioned as the UILabel has increased in height from the one displayed within XIB file UIView's下方UILabel被重新定位为UILabel已高度从XIB文件中显示的一个增加
  4. User presses 'View More' and the UILabel is resized to fit all the available text and increases in height and all UIView's are repositioned further down the screen accordingly 用户按“查看更多”, UILabel调整大小以适合所有可用文本并增加高度,并且所有UIView都相应地在屏幕下方重新定位
  5. User presses 'View Less' and the UILabel decreases in height, hiding more text and UIView's move back up accordingly. 用户按下“少看” UILabel的高度减小,隐藏更多文本,并且UIView's地向上移动。

Thanks, hope this is explained clearly! 谢谢,希望这能清楚解释! I'm attempting to do as much in the XIB file as possible to reduce code, but all my UI elements are still accessible via IBOutlets if that proves to be the only solution (I hope not) . 我试图在XIB文件中做尽可能多的事情以减少代码,但是如果事实证明这是唯一的解决方案,那么我所有的UI元素仍然可以通过IBOutlets访问(我希望不是)

Note: I am required to support iOS4.3 and above, so am unable to use iOS6 auto-layout functionality. 注意:我需要支持iOS4.3及更高版本,因此无法使用iOS6自动布局功能。

Autoresizing usually only enforces subview layout when you alter the size of the parent view. 自动调整大小通常仅在更改父视图的大小时才强制执行子视图布局。 You can link most of it up through IBOutlets, however, when you tap the view more button you will have to tell your parent view to resize. 您可以通过IBOutlets将其大部分链接起来,但是,当您点击查看更多按钮时,您将不得不告诉您的父视图调整大小。

If you are willing to, you could use these extensions on UIView: http://glassofcocoa.com/blog/2012/11/14/uiview-extensions/ and you could do it manually quite easily and with an animations - would look something like: 如果愿意,您可以在UIView上使用这些扩展名: http : //glassofcocoa.com/blog/2012/11/14/uiview-extensions/,并且可以很容易地手动完成并带有动画-看起来会有些喜欢:

//assume you have a BOOL value wether toggled or not called 'isViewingMore' and a stored startHeight
- (IBAction)didTapViewMore:(id)sender
{
    if (!_isViewingMore)
    {
        _isViewingMore = YES;
        CGSize constraint = CGSizeMake(_myLabel.frame.size.width, CGFLOAT_MAX);
        CGFloat lblheight = [@"" sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap].height;
        _myLabel.height = lblHeight;
    }
    else
    {
        _isViewingMore = NO;
        myView.height = startHeight
    }
    UIView *lastView = nil;
    for (UIView *view in self.view.subviews)
    {
        if ((id)view != (id)_myLabel)
        {
            if (lastView == nil)
            {
                view.top = _myLabel.bottom;
            }
            else
            {
                view.top = lastView.bottom;
            }
            lastView = view;
        }
    }
}

If you want to take the IB only route, you will need to put your subviews in one parent view and resize the parent view - this should tell the children to update their positioning/size. 如果要采用仅IB路线,则需要将子视图放在一个父视图中并调整父视图的大小-这应该告诉子项更新其位置/大小。

Hope this helps. 希望这可以帮助。

I was unable to use the autosizing features in the end, so I resolved the issue by creating a UIView that acted as a container for all the subviews that were BELOW the UILabel that expanded. 最后,我无法使用自动调整大小功能,因此我通过创建一个UIView来解决该问题,该UIView充当了扩展的UILabel之下所有子视图的容器。

I was then able to create a helper method that animated the frame adjusting according to the difference between the full size UILabel and the half sized label. 然后,我能够创建一个辅助方法,根据全尺寸UILabel和半尺寸标签之间的差异对框架进行动画处理。

- (void)repositionMoveableViewContainerWithFrame:(CGRect)newFrame animated:(BOOL)animated
{
    CGRect newContainerPosition = self.moveableViewsContainer.frame;
    CGFloat difference = (newFrame.size.height - self.adjustableLabel.frame.size.height);
    newContainerPosition.origin.y = newContainerPosition.origin.y + difference;

    if (animated) {
        // Adjust frame animated
    } else {
        // Adjust frame without animation.
    }    
}

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

相关问题 如何使用Interface Builder在视图中重新定位和调整子视图的大小? - How to reposition and resize subviews in a view using Interface Builder? 在Xcode界面构建器中创建动态调整大小的UILabel - Creating a dynamically sizing UILabel in the Xcode interface builder 如何在UIView中动态添加/删除按钮并重新定位其下方的按钮并垂直调整视图的大小? - How can I dynamically add/remove a button in a UIView and reposition the buttons below it and vertically resize the view? UIScrollView动态调整UILabel的大小 - UIScrollView dynamically resize UILabel 使用接口构建器和XIB,UIlabel以及不同颜色的文本 - Using Interface builder and XIB, UIlabel with text in different colors 使用Interface Builder创建的UILabel框架的原点错误 - UILabel created using Interface Builder wrong origin for the frame 使用Interface Builder和Autolayout在UIScrollView中正确调整UILabel的大小? - Correctly Size UILabel inside UIScrollView using Interface Builder and Autolayout? 自动布局以动态调整UILabel的大小不起作用 - Autolayout to Dynamically Resize UILabel not working iOS动态调整样式的UILabel的大小 - iOS resize styled UILabel dynamically Swift:动态UIView,可在调整大小后将UIView推入其下 - Swift: Dynamic UIView that pushes UIViews below it after a resize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM