如何根据文本长度计算UILabel宽度?

She*_*lam 137 objective-c uikit uilabel ios swift

我想在UILabel旁边显示一个图像,但是UILabel有可变的文本长度,所以我不知道在哪里放置图像.我怎么能做到这一点?

Aar*_*ers 189

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                        constrainedToSize:maximumLabelSize 
                        lineBreakMode:yourLabel.lineBreakMode]; 
Run Code Online (Sandbox Code Playgroud)

什么是 - [NSString sizeWithFont:forWidth:lineBreakMode:]有用吗?

这个问题可能有你的答案,对我有用.


2014年,我在这个新版本中进行了编辑,基于Norbert的超便利评论!这做了一切.干杯

// yourLabel is your UILabel.

float widthIs = 
 [self.yourLabel.text
  boundingRectWithSize:self.yourLabel.frame.size                                           
  options:NSStringDrawingUsesLineFragmentOrigin
  attributes:@{ NSFontAttributeName:self.yourLabel.font }
  context:nil]
   .size.width;

NSLog(@"the width of yourLabel is %f", widthIs);
Run Code Online (Sandbox Code Playgroud)

  • 请注意:自iOS7以来,这已被弃用.现在首选的方法是:`[yourString boundingRectWithSize:maximumLabelSize选项:NSStringDrawingUsesLineFragmentOrigin属性:@ {NSFontAttributeName:yourLabel.font} context:nil];` (41认同)
  • 您还可以使用IntrinsicContentSize属性.我不太喜欢Objective-c,但它应该是这样的:`self.yourLabel.intrinsicContentSize`这将给你标签内容的大小,所以你可以从那里获得宽度. (16认同)

Jak*_*lář 148

yourLabel.intrinsicContentSize.widthfor Objective-C/Swift

  • 活泉!到目前为止,这是最简单的方法. (4认同)
  • 奇迹般有效 (4认同)
  • 从Swift 4.2开始,它仍然运作良好:) (3认同)
  • 非常适合我,还可以使用自定义字体! (2认同)
  • 获取动态标签宽度的完美答案 (2认同)

小智 49

在迅速

 yourLabel.intrinsicContentSize().width 
Run Code Online (Sandbox Code Playgroud)

  • 此答案仅对已布局的视图有效 (3认同)
  • 最简单的答案!! (2认同)

Che*_*noy 33

iOS 6及更低版本的所选答案正确无误.

在iOS 7中,sizeWithFont:constrainedToSize:lineBreakMode:已被弃用.现在建议您使用boundingRectWithSize:options:attributes:context:.

CGRect expectedLabelSize = [yourString boundingRectWithSize:sizeOfRect
                                                    options:<NSStringDrawingOptions>
                                                 attributes:@{
                                                    NSFontAttributeName: yourString.font
                                                    AnyOtherAttributes: valuesForAttributes
                                                 }
                                                    context:(NSStringDrawingContext *)];
Run Code Online (Sandbox Code Playgroud)

请注意,返回值CGRect不是a CGSize.希望这对在iOS 7中使用它的人有所帮助.


jar*_*ora 12

在iOS8中,sizeWithFont已被弃用,请参阅

CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:@{NSFontAttributeName : [UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];
Run Code Online (Sandbox Code Playgroud)

您可以在sizeWithAttributes中添加所需的所有属性.您可以设置的其他属性:

- NSForegroundColorAttributeName
- NSParagraphStyleAttributeName
- NSBackgroundColorAttributeName
- NSShadowAttributeName
Run Code Online (Sandbox Code Playgroud)

等等.但可能你不需要其他人


小智 8

CGRect rect = label.frame;
rect.size = [label.text sizeWithAttributes:@{NSFontAttributeName : [UIFont fontWithName:label.font.fontName size:label.font.pointSize]}];
label.frame = rect;
Run Code Online (Sandbox Code Playgroud)

  • 这并没有提供问题的答案.要批评或要求作者澄清,请在帖子下方留言 - 您可以随时评论自己的帖子,一旦您有足够的[声誉](http://stackoverflow.com/help/whats-reputation),您将能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment). (2认同)

iOS*_*fee 6

Swift 4 Answer谁在使用约束

label.text = "Hello World"

var rect: CGRect = label.frame //get frame of label
rect.size = (label.text?.size(attributes: [NSFontAttributeName: UIFont(name: label.font.fontName , size: label.font.pointSize)!]))! //Calculate as per label font
labelWidth.constant = rect.width // set width to Constraint outlet
Run Code Online (Sandbox Code Playgroud)

Swift 5 Answer正在使用约束

label.text = "Hello World"

var rect: CGRect = label.frame //get frame of label
rect.size = (label.text?.size(withAttributes: [NSAttributedString.Key.font: UIFont(name: label.font.fontName , size: label.font.pointSize)!]))! //Calculate as per label font
labelWidth.constant = rect.width // set width to Constraint outlet
Run Code Online (Sandbox Code Playgroud)

  • 伟大的!方便根据文本计算UIButton的宽度,其中intrinsicContentSize.width并不总是正确工作。 (2认同)