Adjust UILabel height depending on the text

前端 未结 30 1771
走了就别回头了
走了就别回头了 2020-11-22 03:53

Consider I have the following text in a UILabel (a long line of dynamic text):

Since the alien army vastly outnumbers the team, players m

相关标签:
30条回答
  • 2020-11-22 04:32

    You were going in the right direction. All you need to do is:

    myUILabel.numberOfLines = 0;
    myUILabel.text = @"Enter large amount of text here";
    [myUILabel sizeToFit];
    
    0 讨论(0)
  • 2020-11-22 04:32

    In iOS 6 Apple has added a property to UILabel that greatly simplifies dynamic vertical resizing of labels: preferredMaxLayoutWidth.

    Using this property in combination with lineBreakMode = NSLineBreakByWordWrapping and sizeToFit method allows easily resize a UILabel instance to the height that accommodates the entire text.

    A quote from iOS documentation:

    preferredMaxLayoutWidth The preferred maximum width (in points) for a multiline label.

    Discussion This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.

    A sample:

    ...
    UILabel *status = [[UILabel alloc] init];
    status.lineBreakMode = NSLineBreakByWordWrapping;
    status.numberOfLines = 5; // limits to 5 lines; use 0 for unlimited.
    
    [self addSubview:status]; // self here is the parent view
    
    status.preferredMaxLayoutWidth = self.frame.size.width; // assumes the parent view has its frame already set.
    
    status.text = @"Some quite lengthy message may go here…";
    [status sizeToFit];
    [status setNeedsDisplay];
    ...
    
    0 讨论(0)
  • 2020-11-22 04:32

    Check this work perfectly without adding Single line of code. (Using Autolayout)

    I made a demo for you according to your requirement. Download it from below link,

    Autoresize UIView and UILabel

    Step by Step Guide :-

    Step 1 :- Set constrain to UIView

    1) Leading 2) Top 3) Trailing (From mainview)

    Step 2 :- Set constrain to Label 1

    1) Leading 2) Top 3) Trailing (From it's superview)

    Step 3 :- Set constrain to Label 2

    1) Leading 2) Trailing (From it's superview)

    Step 4 :- Most tricky give botton to UILabel from UIView .

    Step 5 :- (Optional) Set constrain to UIButton

    1) Leading 2) Bottom 3) Trailing 4) Fixed Height (From mainview)

    Output :-

    Note :- Make sure you have set Number of lines =0 in Label property.

    I hope this info enough to understand Autoresize UIView according to UILabel's height and Autoresize UILabel According to text.

    0 讨论(0)
  • 2020-11-22 04:34

    My code:

    UILabel *label      = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.text          = text;
    label.textAlignment = NSTextAlignmentCenter;
    label.font          = [UIFont fontWithName:_bodyTextFontFamily size:_bodyFontSize];
    
    CGSize size = [label sizeThatFits:CGSizeMake(width, MAXFLOAT)];
    
    
    float height        = size.height;
    label.frame         = CGRectMake(x, y, width, height);
    
    0 讨论(0)
  • 2020-11-22 04:35

    And for those that are migrating to iOS 8, here is a class extension for Swift:

    extension UILabel {
    
        func autoresize() {
            if let textNSString: NSString = self.text {
                let rect = textNSString.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max),
                    options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                    attributes: [NSFontAttributeName: self.font],
                    context: nil)
                self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, rect.height)
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 04:36

    My approach to compute the dynamic height of UILabel.

        let width = ... //< width of this label 
        let text = ... //< display content
    
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.preferredMaxLayoutWidth = width
    
        // Font of this label.
        //label.font = UIFont.systemFont(ofSize: 17.0)
        // Compute intrinsicContentSize based on font, and preferredMaxLayoutWidth
        label.invalidateIntrinsicContentSize() 
        // Destination height
        let height = label.intrinsicContentSize.height
    

    Wrap to function:

    func computeHeight(text: String, width: CGFloat) -> CGFloat {
        // A dummy label in order to compute dynamic height.
        let label = UILabel()
    
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.font = UIFont.systemFont(ofSize: 17.0)
    
        label.preferredMaxLayoutWidth = width
        label.text = text
        label.invalidateIntrinsicContentSize()
    
        let height = label.intrinsicContentSize.height
        return height
    }
    
    0 讨论(0)
提交回复
热议问题