如何调整和调整UILabel的宽度以适应文本大小?

时间:2022-02-24 21:10:35

In my project, there is a UILabel with text. The font size is 16pt. The text contents are changed depending on different cases. I hope it can automatically adjust the width of UILabel to fit the total width of texts without stretching.

在我的项目中,有一个带有文本的UILabel。字体大小为16pt。文本内容根据不同的情况而变化。我希望它能自动调整UILabel的宽度,以适应文本的总宽度而不需要拉伸。

Is it possible?

是可能的吗?

8 个解决方案

#1


107  

This assumes you have already set the font:

假设您已经设置了字体:

label.text = @"some text";
[label sizeToFit];

You will also need to define a maximum width, and tell your program what to do if sizeToFit gives you a width greater than that maximum.

您还需要定义一个最大宽度,并告诉您的程序,如果sizeToFit为您提供的宽度大于最大宽度,该怎么办。

#2


21  

Here's how to do it, suppose the following messageLabel is the label you want to have the desired effect. Now, try these simple line of codes:

这里是如何做到这一点的,假设下面的messageLabel是您想要达到的效果的标签。现在,试试这些简单的代码行:

    // Set width constraint for label; it's actually the width of your UILabel
    CGFloat constrainedWidth = 240.0f;
    // Calculate space for the specified string
    CGSize sizeOfText = [yourText sizeWithFont:yourFont constrainedToSize:CGSizeMake(constrainedWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,constrainedWidth,sizeOfText.height)];
    messageLabel.text = yourText;
    messageLabel.numberOfLines = 0;// This will make the label multiline

#3


12  

NSString *txt1=@"I am here.";
CGSize stringsize1 = [txt1 sizeWithFont:[UIFont systemFontOfSize:14]]; 
[label setFrame:CGRectMake(x,y,stringsize1.width,hieght)];
[label setText:txt1];

#4


7  

I see three options here.

这里有三个选项。

First, make label's size big enough to hold any text. That's most simple, but does not always work well - depends on its surrounding views.

首先,使标签的大小足以容纳任何文本。这是最简单的,但并不总是有效——取决于它周围的视图。

Second, Label can adapt size of the font for longer text (adjustsFontSizeToFitWidth property). This is often not desirable, different fonts in elements might look ugly.

其次,标签可以调整字体的大小以适应较长的文本(如调整字体大小)。这通常是不可取的,元素中的不同字体可能看起来很难看。

Last option is to programmatically resize the label according to its currently holding text. To calculate the size required to hold the text with current font use something like this:

最后一个选项是根据标签当前保存的文本以编程方式调整标签的大小。要计算保持当前字体的文本所需的大小,请使用以下方法:

CGSize textSize = [[someLabel text] sizeWithFont:[someLabel font] forWidth:someLabel.bounds.size.width lineBreakMode:UILineBreakModeWordWrap];

#5


2  

If you set your font and its size already and if you have your frame defined, try using the following for these two common conditions:

如果您已经设置了字体和大小,如果您已经定义了框架,请尝试使用以下两个常见的条件:

if (label.text.length > maxCharPerLine) [label setNumberOfLines:0]; // infinite lines
else [label setNumberOfLines:1]; // one line only

// Adjust your font size to fit your desired width.
[label setAdjustsFontSizeToFitWidth:YES];

#6


1  

As sizeWithFont is depreciated in IOS 7.0 then you below code

因为sizeWithFont在ios7.0中是贬值的,那么你在代码下面

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)



 if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        // code here for iOS 5.0,6.0 and so on
        CGSize fontSize = [itemCat_text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]];
    } else {
        // code here for iOS 7.0
         fontSize = [itemCat_text sizeWithAttributes:
                           @{NSFontAttributeName:
                                 [UIFont fontWithName:@"Helvetica" size:12]}];
    }

#7


1  

Using auto LayOut:

使用自动布局:

如何调整和调整UILabel的宽度以适应文本大小?

On ViewController:

ViewController:

override func viewDidLoad() {
        super.viewDidLoad()

self.sampleLabel.text = "Electrical Maintenance and Repair"
            self.sampleLabel..sizeToFit()

}

#8


1  

Follow this.

遵循这一点。

CGSize stringsize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]]; 
[label setFrame:CGRectMake(x,y,stringsize.width,height)];
[label setText: yourString];

#1


107  

This assumes you have already set the font:

假设您已经设置了字体:

label.text = @"some text";
[label sizeToFit];

You will also need to define a maximum width, and tell your program what to do if sizeToFit gives you a width greater than that maximum.

您还需要定义一个最大宽度,并告诉您的程序,如果sizeToFit为您提供的宽度大于最大宽度,该怎么办。

#2


21  

Here's how to do it, suppose the following messageLabel is the label you want to have the desired effect. Now, try these simple line of codes:

这里是如何做到这一点的,假设下面的messageLabel是您想要达到的效果的标签。现在,试试这些简单的代码行:

    // Set width constraint for label; it's actually the width of your UILabel
    CGFloat constrainedWidth = 240.0f;
    // Calculate space for the specified string
    CGSize sizeOfText = [yourText sizeWithFont:yourFont constrainedToSize:CGSizeMake(constrainedWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,constrainedWidth,sizeOfText.height)];
    messageLabel.text = yourText;
    messageLabel.numberOfLines = 0;// This will make the label multiline

#3


12  

NSString *txt1=@"I am here.";
CGSize stringsize1 = [txt1 sizeWithFont:[UIFont systemFontOfSize:14]]; 
[label setFrame:CGRectMake(x,y,stringsize1.width,hieght)];
[label setText:txt1];

#4


7  

I see three options here.

这里有三个选项。

First, make label's size big enough to hold any text. That's most simple, but does not always work well - depends on its surrounding views.

首先,使标签的大小足以容纳任何文本。这是最简单的,但并不总是有效——取决于它周围的视图。

Second, Label can adapt size of the font for longer text (adjustsFontSizeToFitWidth property). This is often not desirable, different fonts in elements might look ugly.

其次,标签可以调整字体的大小以适应较长的文本(如调整字体大小)。这通常是不可取的,元素中的不同字体可能看起来很难看。

Last option is to programmatically resize the label according to its currently holding text. To calculate the size required to hold the text with current font use something like this:

最后一个选项是根据标签当前保存的文本以编程方式调整标签的大小。要计算保持当前字体的文本所需的大小,请使用以下方法:

CGSize textSize = [[someLabel text] sizeWithFont:[someLabel font] forWidth:someLabel.bounds.size.width lineBreakMode:UILineBreakModeWordWrap];

#5


2  

If you set your font and its size already and if you have your frame defined, try using the following for these two common conditions:

如果您已经设置了字体和大小,如果您已经定义了框架,请尝试使用以下两个常见的条件:

if (label.text.length > maxCharPerLine) [label setNumberOfLines:0]; // infinite lines
else [label setNumberOfLines:1]; // one line only

// Adjust your font size to fit your desired width.
[label setAdjustsFontSizeToFitWidth:YES];

#6


1  

As sizeWithFont is depreciated in IOS 7.0 then you below code

因为sizeWithFont在ios7.0中是贬值的,那么你在代码下面

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)



 if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        // code here for iOS 5.0,6.0 and so on
        CGSize fontSize = [itemCat_text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]];
    } else {
        // code here for iOS 7.0
         fontSize = [itemCat_text sizeWithAttributes:
                           @{NSFontAttributeName:
                                 [UIFont fontWithName:@"Helvetica" size:12]}];
    }

#7


1  

Using auto LayOut:

使用自动布局:

如何调整和调整UILabel的宽度以适应文本大小?

On ViewController:

ViewController:

override func viewDidLoad() {
        super.viewDidLoad()

self.sampleLabel.text = "Electrical Maintenance and Repair"
            self.sampleLabel..sizeToFit()

}

#8


1  

Follow this.

遵循这一点。

CGSize stringsize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]]; 
[label setFrame:CGRectMake(x,y,stringsize.width,height)];
[label setText: yourString];