ios iPhone - 根据文字调整 UILabel 宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13152262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:01:08  来源:igfitidea点击:

iPhone - Adjust UILabel width according to the text

iphoneobjective-ciosuilabel

提问by Dev

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.

其实我有两个 UIlabels。我需要把这两个放在附近。但是如果第一个标签的文字太小,就会有很大的差距。我想消除这个差距。

回答by NANNAV

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

回答by Sihad Begovic

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:

函数 sizeWithFont:在 iOS 7.0 中已弃用,因此您必须在sizeWithAttributes:iOS 7.0+ 中使用。同样为了支持旧版本,可以使用以下代码:

    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:

Apple 文档推荐ceil()在结果上使用函数sizeWithAttributes:

"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."

“此方法返回小数大小;要使用返回的大小来调整视图的大小,您必须使用 ceil 函数将其值提高到最接近的更高整数。”

sizeWithAttributes

大小带属性

回答by Vinayak

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

回答by iDev

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,

您也可以尝试[label sizeToFit];使用此方法,您可以将两个标签的框架设置为,

[firstLabel sizeToFit];
[secondLabel sizeToFit];
secondLabel.frame = CGRectMake(CGRectGetMaxX(firstLabel.frame), secondLabel.origin.y, secondLabel.frame.size.width, secondLabel.frame.size.height);

回答by Shanmugaraja G

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

sizeWithFont constrainedToSize:lineBreakMode:是使用的原始方法。以下是如何使用它的示例:

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

回答by jenish

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

如果您在视图或 xib 或单元格中使用约束,请使用 to

[LBl sizeToFit];

if its not working then

如果它不工作那么

dispatch_async(dispatch_get_main_queue(), ^{
[LBl sizeToFit];
});

回答by Ravi Raman

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;