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

时间:2010-08-20 01:06:23

标签: ios objective-c swift uikit uilabel

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

8 个答案:

答案 0 :(得分:186)

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                        constrainedToSize:maximumLabelSize 
                        lineBreakMode:yourLabel.lineBreakMode]; 

What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?

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


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

答案 1 :(得分:131)

yourLabel.intrinsicContentSize.width Objective-C / Swift

答案 2 :(得分:48)

在swift中

 yourLabel.intrinsicContentSize().width 

答案 3 :(得分:32)

所选答案适用于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 *)];

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

答案 4 :(得分:11)

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

CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:@{NSFontAttributeName : [UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];

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

- NSForegroundColorAttributeName
- NSParagraphStyleAttributeName
- NSBackgroundColorAttributeName
- NSShadowAttributeName

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

答案 5 :(得分:6)

CGRect rect = label.frame;
rect.size = [label.text sizeWithAttributes:@{NSFontAttributeName : [UIFont fontWithName:label.font.fontName size:label.font.pointSize]}];
label.frame = rect;

答案 6 :(得分:2)

Swift 4回答谁在使用约束

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

答案 7 :(得分:1)

这是我在应用其他SO帖子之后提出的一些内容,包括Aaron的链接:

    AnnotationPin *myAnnotation = (AnnotationPin *)annotation;

    self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
    self.backgroundColor = [UIColor greenColor];
    self.frame = CGRectMake(0,0,30,30);
    imageView = [[UIImageView alloc] initWithImage:myAnnotation.THEIMAGE];
    imageView.frame = CGRectMake(3,3,20,20);
    imageView.layer.masksToBounds = NO;
    [self addSubview:imageView];
    [imageView release];

    CGSize titleSize = [myAnnotation.THETEXT sizeWithFont:[UIFont systemFontOfSize:12]];
    CGRect newFrame = self.frame;
    newFrame.size.height = titleSize.height + 12;
    newFrame.size.width = titleSize.width + 32;
    self.frame = newFrame;
    self.layer.borderColor = [UIColor colorWithRed:0 green:.3 blue:0 alpha:1.0f].CGColor;
    self.layer.borderWidth = 3.0;

    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(26,5,newFrame.size.width-32,newFrame.size.height-12)];
    infoLabel.text = myAnnotation.title;
    infoLabel.backgroundColor = [UIColor clearColor];
    infoLabel.textColor = [UIColor blackColor];
    infoLabel.textAlignment = UITextAlignmentCenter;
    infoLabel.font = [UIFont systemFontOfSize:12];

    [self addSubview:infoLabel];
    [infoLabel release];

在这个例子中,我将一个自定义引脚添加到MKAnnotation类,根据文本大小调整UILabel的大小。它还在视图的左侧添加了一个图像,因此您可以看到一些代码管理正确的间距来处理图像和填充。

关键是使用CGSize titleSize = [myAnnotation.THETEXT sizeWithFont:[UIFont systemFontOfSize:12]];,然后重新定义视图的尺寸。您可以将此逻辑应用于任何视图。

虽然Aaron的答案适用于某些人,但它对我不起作用。这是一个更详细的解释,如果你想要一个带有图像和可调整大小的UILabel的更动态的视图,你应该在去其他任何地方之前立即尝试。我已经为你完成了所有的工作!!

相关问题