19

如何根据文本调整标签宽度?如果文本长度小,我希望标签宽度小...如果文本长度小,我希望标签宽度根据该文本长度。可能吗?

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

4

7 回答 7

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; 
于 2012-10-31T06:38:24.477 回答
12

函数 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);
    }

Apple 文档建议ceil()对结果使用函数:sizeWithAttributes:

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

sizeWithAttributes

于 2014-04-16T21:09:39.650 回答
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
于 2016-03-11T10:28:43.030 回答
3

试试这些选项,

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

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

[firstLabel sizeToFit];
[secondLabel sizeToFit];
secondLabel.frame = CGRectMake(CGRectGetMaxX(firstLabel.frame), secondLabel.origin.y, secondLabel.frame.size.width, secondLabel.frame.size.height);
于 2012-10-31T06:19:25.433 回答
2

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;
于 2012-10-31T06:27:05.960 回答
1

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

[LBl sizeToFit];

如果它不工作那么

dispatch_async(dispatch_get_main_queue(), ^{
[LBl sizeToFit];
});
于 2015-10-29T13:07:35.067 回答
0

尝试以下操作:

/* 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;
于 2012-10-31T07:27:13.173 回答