How to adjust a label size to fit the length of the text

I have searched the solution to this in the past QAs, but could not find the right one.
Does anyone know how to adjust a UILabel size dynamically to fit the text length?
I have uploaded the screen shot of what I don't want(1st line) and what I want(2nd line).
I'd appreciate any clues, advice or code sample. Thank you. 在这里输入图像描述


What you are searching is the UILabel method sizeToFit

I can try to explain to you, but the best answer to know how to work with UILabel is that: https://stackoverflow.com/a/1054681/666479


Xcode 8 and iOS 10

This is quite easy to do with Auto Layout. No need to do anything in code.

在这里输入图像描述

Use Auto Layout

Use auto layout to pin each label's top and left edges only. Don't add constraints for the width and height. The view's intrinsic content size will take care of that.

在这里输入图像描述

Here is what the constraints look like:

在这里输入图像描述

Code

There is nothing at all special about the code. No need to use sizeToFit or anything like that.

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var labelOne: UILabel!
    @IBOutlet weak var labelTwo: UILabel!
    @IBOutlet weak var labelThree: UILabel!

    @IBAction func changeTextButtonTapped(_ sender: UIButton) {

        labelOne.text = "David"
        labelTwo.text = "met"
        labelThree.text = "her"
    }
}

Notes

  • This answer has been retested with Xcode 8, iOS 10, and Swift 3.
  • See my other answer if you want multi-line resizing.
  • Thanks to this answer for setting me on the right track.

  • Use This Extended UILabel class:

    //
    //  UILabelExtended.h
    
    //
    //  Created by Prateek on 6/18/11.
    
    
    
    #import <Foundation/Foundation.h>
    
    /*  **********************************************************************************************
            This class inherit the class UILabel and extend the features of UILabel. 
        ********************************************************************************************** */
    @interface UILabelExtended : UILabel {
       __unsafe_unretained id  customDelegate;
        id  objectInfo;
        SEL selector;
    }
    
    @property (nonatomic,assign) SEL selector;;
    @property (nonatomic,assign) id  customDelegate;
    @property (nonatomic,retain) id  objectInfo;
    @end
    
    @interface UILabel(UILabelCategory)
    - (void)setHeightOfLabel;
    - (void)setWidthOfLabel;
    - (void)setHeightOfLabelWithMaxHeight:(float)maxHeight;
    - (void)setWidthOfLabelWithMaxWidth:(float)maxWidth ;
    @end
    

    UILabelExtended.m

    //
    //  Created by Prateek on 6/18/11.
    
    //
    
    #import "UILabelExtended.h"
    
    
    @implementation UILabelExtended
    @synthesize selector,customDelegate, objectInfo;
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if(self.selector)
            if([self.customDelegate respondsToSelector:self.selector]) {
                [self.customDelegate performSelector:self.selector withObject:self];
                return;
            }
    }
    
    - (void)dealloc {
    
        self.customDelegate = nil;
        self.selector = NULL;
        self.objectInfo = nil;
    }
    @end
    
    
    @implementation UILabel(UILabelCategory)
    
    - (void)setHeightOfLabel {
        UILabel* label = self;
    
        //get the height of label content
        CGFloat height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping].height;
        //set the frame according to calculated height
        CGRect frame = label.frame;
        if([label.text length] > 0) {
    
            frame.size.height = height;
        } 
        else {
            frame.size.height = 0;
        }
        label.frame = frame;
    }
    
    
    - (void)setWidthOfLabel {
        UILabel* label = self;
    
            //get the height of label content
        CGFloat width = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(99999, label.bounds.size.height) lineBreakMode:NSLineBreakByWordWrapping].width;
            //set the frame according to calculated height
        CGRect frame = label.frame;
        if([label.text length] > 0) {
    
            frame.size.width = width+5;
        } 
        else {
            frame.size.width = 0;
        }
        label.frame = frame;
    }
    
    - (void)setHeightOfLabelWithMaxHeight:(float)maxHeight {
        UILabel* label = self;
    
        //get the height of label content
        CGFloat height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, maxHeight) lineBreakMode:NSLineBreakByWordWrapping].height;
        //set the frame according to calculated height
        CGRect frame = label.frame;
    
        if([label.text length] > 0) {
            if (height > maxHeight) {
                frame.size.height = maxHeight;
            }
            else {
                frame.size.height = height;
            }
    
        } 
        else {
            frame.size.height = 0;
        }
        label.frame = frame;
    }
    
    - (void)setWidthOfLabelWithMaxWidth:(float)maxWidth  {
        UILabel* label = self;
    
        //get the height of label content
        CGFloat width = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(99999, label.bounds.size.height) lineBreakMode:NSLineBreakByWordWrapping].width;
        //set the frame according to calculated height
        CGRect frame = label.frame;
        if([label.text length] > 0) {
    
            if (width > maxWidth) {
                frame.size.width = maxWidth;
            }
            else {
                frame.size.width = width;
            }
        } 
        else {
            frame.size.width = 0;
        }
        label.frame = frame;
    }
    @end
    

    Use Methods: 1) set text of UILabel 2) [yourLBLObj setHeightOfLabel]; or [yourLBLObj setWidthOfLabel]; It will automatically set Height or Width according to text.

    链接地址: http://www.djcxy.com/p/28196.html

    上一篇: 调整UILabel以适应?

    下一篇: 如何调整标签大小以适应文本的长度