19

How can I adjust the label Width according to the text? If text length is small I want the label width small...If text length is small I want the label width according to that text length. Is it possible?

Actually I have Two UIlabels. I need to place these two nearby. But if the first label's text is too small there will be a big gap. I want to remove this gap.

iDev
  • 23,310
  • 7
  • 60
  • 85
Dev
  • 3,885
  • 10
  • 39
  • 65

6 Answers6

38
//use this for custom font
CGFloat width =  [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width;

//use this for system font 
CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;

label.frame = CGRectMake(point.x, point.y, width,height);

//point.x, point.y -> origin for label;
//height -> your label height; 
Usman Nisar
  • 3,031
  • 33
  • 41
NANNAV
  • 4,875
  • 4
  • 32
  • 50
12

Function sizeWithFont: is deprecated in iOS 7.0, so you have to use sizeWithAttributes: for iOS 7.0+. Also to suport older versions, this code below can be used:

    CGFloat width;
    if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0)
    {
        width = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0 ]].width;
    }
    else
    {
        width = ceil([text sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:16.0]}].width);
    }

Using function ceil() on result of sizeWithAttributes: is recommended by Apple documentation:

"This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function."

sizeWithAttributes

Sihad Begovic
  • 1,907
  • 1
  • 31
  • 33
5
    // In swift 2.0
    let lblDescription = UILabel(frame: CGRectMake(0, 0, 200, 20))
    lblDescription.numberOfLines = 0
    lblDescription.text = "Sample text to show its whatever may be"
    lblDescription.sizeToFit()

    // Its automatically Adjust the height
Vinayak
  • 329
  • 1
  • 3
  • 10
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Tobi Nary Mar 12 '16 at 11:55
  • 1
    @Vinayak mahadev provide some explanation about the code snippet – user23790 Mar 14 '16 at 08:42
  • Hi SmokeDispenser, Just giving lblDescription.sizeToFit() Automatically it will Expanding UILabel Height. We no need to calculate anything. – Vinayak Mar 14 '16 at 08:48
3

Try these options,

UIFont *myFont = [UIFont boldSystemFontOfSize:15.0];
// Get the width of a string ...
CGSize size = [@"Some string here" sizeWithFont:myFont];

// Get the width of a string when wrapping within a particular width
NSString *mystring = @"some strings some string some strings...";
CGSize size = [mystring sizeWithFont:myFont
                              forWidth:150.0
                lineBreakMode:UILineBreakModeWordWrap];

You can also try with [label sizeToFit]; Using this method, you can set frame of two labels as,

[firstLabel sizeToFit];
[secondLabel sizeToFit];
secondLabel.frame = CGRectMake(CGRectGetMaxX(firstLabel.frame), secondLabel.origin.y, secondLabel.frame.size.width, secondLabel.frame.size.height);
iDev
  • 23,310
  • 7
  • 60
  • 85
1

just use to if you using constrain in your view or xib or cell

[LBl sizeToFit];

if its not working then

dispatch_async(dispatch_get_main_queue(), ^{
[LBl sizeToFit];
});
jenish
  • 268
  • 3
  • 11
  • Updating the UI on the main thread is the key here. Even when we are not using Blocks, its a good to have to make sure that all updates happen in the main thread. – Apple_iOS0304 Jun 02 '16 at 12:40
0

Try the following:

/* Consider these two labels as the labels that you use, 
and that these labels have been initialized */

UILabel* firstLabel;
UILabel* secondLabel;

CGSize labelSize = [firstLabel.text sizeWithFont:[UIFont systemFontOfSize:12]]; 
//change the font size, or font as per your requirements

CGRect firstLabelRect = firstLabel.frame;

firstLabelRect.size.width = labelSize.width; 
//You will get the width as per the text in label

firstLabel.frame = firstLabelRect;


/* Now, let's change the frame for the second label */
CGRect secondLabelRect;

CGFloat x = firstLabelRect.origin.x;
CGFloat y = firstLabelRect.origin.y;

x = x + labelSize.width + 20; //There are some changes here.

secondLabelRect = secondLabel.frame;
secondLabelRect.origin.x = x;
secondLabelRect.origin.y = y; 

secondLabel.frame = secondLabelRect;
Ravi Raman
  • 1,070
  • 9
  • 16