iPhone - Adjust UILabel width according to the text

喜你入骨 提交于 2020-01-20 17:02:26

问题


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.


回答1:


//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; 



回答2:


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




回答3:


    // 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



回答4:


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);



回答5:


sizeWithFont constrainedToSize:lineBreakMode: is the original method to use. Here is an example of how to use it is below:

//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;



回答6:


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];
});



回答7:


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;


来源:https://stackoverflow.com/questions/13152262/iphone-adjust-uilabel-width-according-to-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!