【问题标题】:iPhone - Adjust UILabel width according to the textiPhone - 根据文字调整 UILabel 宽度
【发布时间】:2012-10-31 06:17:16
【问题描述】:

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

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

【问题讨论】:

  • 检查我的第二部分答案。
  • 尝试属性文本文本。

标签: iphone objective-c ios uilabel


【解决方案1】:
//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; 

【讨论】:

  • 效果很好!谢谢!
  • 在 iOS 7.0 中已弃用
【解决方案2】:

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

    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 文档建议在 sizeWithAttributes: 的结果上使用函数 ceil()

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

sizeWithAttributes

【讨论】:

  • 用旧方法做这件事有什么意义?为什么不总是使用 sizeWithAttributes?
【解决方案3】:
    // 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

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
  • @Vinayak mahadev 提供了一些关于代码 sn-p 的解释
  • 嗨 SmokeDispenser,只要给 lblDescription.sizeToFit() 自动它会扩展 UILabel 高度。我们不需要计算任何东西。
【解决方案4】:

试试这些选项,

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

【讨论】:

    【解决方案5】:

    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;
    

    【讨论】:

      【解决方案6】:

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

      [LBl sizeToFit];
      

      如果它不起作用,那么

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

      【讨论】:

      • 在主线程上更新 UI 是这里的关键。即使我们不使用 Blocks,也必须确保所有更新都发生在主线程中。
      【解决方案7】:

      尝试以下方法:

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

      【讨论】:

      • 您面临的问题是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多