代码之家  ›  专栏  ›  技术社区  ›  arachide

如何调整并使UILabel的宽度适合文本大小?

  •  46
  • arachide  · 技术社区  · 14 年前

    UILabel 有文字。字体大小是16磅。文本内容根据不同的情况而改变。我希望它能自动调整 UILabel公司

    有可能吗?

    8 回复  |  直到 7 年前
        1
  •  110
  •   William Jockusch    14 年前

    这假设您已经设置了字体:

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

    您还需要定义最大宽度,并告诉程序如果SiZeToFIT给您的宽度大于最大值,该怎么做。

        2
  •  21
  •   Forge Nand Parikh    7 年前

    下面是如何做到的,假设 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
  •  13
  •   Vikas Pandey    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
  •   Forge Nand Parikh    7 年前

    首先,使标签的大小足以容纳任何文本。这是最简单的,但并不总是很好的工作-取决于其周围的看法。

    其次,标签可以调整字体的大小以适应较长的文本( adjustsFontSizeToFitWidth

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

    CGSize textSize = [[someLabel text] sizeWithFont:[someLabel font] forWidth:someLabel.bounds.size.width lineBreakMode:UILineBreakModeWordWrap];
    
        5
  •  2
  •   Will Von Ullrich    8 年前

    如果已经设置了字体及其大小,并且定义了边框,请尝试对这两种常见情况使用以下方法:

    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
  •   9to5ios    8 年前

    由于iOS7.0对sizeWithFont进行了折旧,因此您需要在下面的代码中

    #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
  •   Forge Nand Parikh    7 年前

    跟着这个。

    CGSize stringsize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]]; 
    [label setFrame:CGRectMake(x,y,stringsize.width,height)];
    [label setText: yourString];
    
        8
  •  1
  •   mokagio codeWhisperer    5 年前

    enter image description here

    在ViewController中:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        sampleLabel.text = "Electrical Maintenance and Repair"
        sampleLabel.sizeToFit()
    }