iPhone - Adjust UILabel width according to the tex

2020-02-19 06:50发布

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.

7条回答
等我变得足够好
2楼-- · 2020-02-19 07:33

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;
查看更多
冷血范
3楼-- · 2020-02-19 07:35

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];
});
查看更多
来,给爷笑一个
4楼-- · 2020-02-19 07:37

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;
查看更多
一纸荒年 Trace。
5楼-- · 2020-02-19 07:42
//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; 
查看更多
时光不老,我们不散
6楼-- · 2020-02-19 07:42

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);
查看更多
淡お忘
7楼-- · 2020-02-19 07:46

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

查看更多
登录 后发表回答