如何将 uilabel 高度固定为动态 [取决于文本]?

How to fix the uilabel height as dynamic[depend upon text]?

我正在尝试将标签高度固定为动态的以取决于文本。

CGSize size = [Str sizeWithAttributes:
                       @{NSFontAttributeName: [UIFont fontWithName:@"OpenSans" size:15]}];
        CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
        NSLog(@"this is height of the label =%@",NSStringFromCGSize(adjustedSize));

我在这里获取宽度和高度,但宽度超出了屏幕尺寸宽度。

我想要的是标签宽度应等于我的屏幕尺寸,高度因文本而异......

Select 你的标签 单击编辑器和 select 大小以适合内容

设置

yourLabel.numberOfLines = 0;
[yourLabel sizeToFit];

为该标签指定特定宽度或前导和尾随 space,以便宽度保持固定,高度根据文本长度增加。

您还可以通过 xib 文件设置 numberOfLines。 1.在xib中点击你的标签。 2. 在对齐选项卡下方的右侧面板中,您可以将 numberOfLines 设置为 1 到 0。

我认为您的自定义字体有问题。您可以使用此代码计算动态大小

NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                       context:nil];
CGSize size = rect.size; 

1.from xib 你需要设置no。此标签的第 0 行:

并使用此方法:

- (CGFloat)getHeightforController:(id)view{
UILabel *tempView =(UILabel *)view;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
context.minimumScaleFactor = 0.8;

float width = tempView.frame.size.width;

width = width * ([[UIScreen mainScreen] bounds].size.width/320);

CGSize size=[tempView.text boundingRectWithSize:CGSizeMake(tempView.frame.size.width, 200)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{ NSFontAttributeName : tempView.font}
                                        context:context].size;
return size.height;
}

像这样调用此方法:

label.text = @"text"
CGRect frame = cell.lblAddress.frame;
frame.size.height = [self getHeightforController:label];
label.frame = frame;

确保在调用此方法之前为标签设置文本

使用自动布局或这样做

yourLabel.numberOfLines = 0;
yourLabel.lineBreakMode = NSLineBreakByWordWrapping;
yourLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;

您可以使用以下代码将标签高度固定为动态,以取决于文本

NSString *str = [NSString stringWithFormat:@"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."];

CGSize maximumLabelSize = CGSizeMake(self.view.frame.size.width - 20, FLT_MAX);

CGSize expectedLabelSize = [str sizeWithFont:[UIFont fontWithName:@"OpenSans" size:15] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];


UILabel *Label = [[UILabel alloc] init];
[Label setTranslatesAutoresizingMaskIntoConstraints:NO];
//set new frame to label.
CGRect newFrame = Label.frame;
newFrame.origin.x = 10;
newFrame.origin.y = 20;
newFrame.size.width = expectedLabelSize.width - 20;
newFrame.size.height = expectedLabelSize.height;
Label.frame = newFrame;
Label.text = str;
Label.lineBreakMode = NSLineBreakByWordWrapping;
Label.numberOfLines = 20;
Label.font = [UIFont fontWithName:@"OpenSans" size:15];
Label.backgroundColor = [UIColor redColor];

[self.view addSubview:Label];

//width constraint
[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label
                                                  attribute:NSLayoutAttributeWidth
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:nil
                                                  attribute: NSLayoutAttributeNotAnAttribute
                                                 multiplier:1
                                                   constant:expectedLabelSize.width - 20]];

//height constraint
[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label
                                                  attribute:NSLayoutAttributeHeight
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:nil
                                                  attribute: NSLayoutAttributeNotAnAttribute
                                                 multiplier:1
                                                   constant: expectedLabelSize.height]];

//center constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view
                                                       attribute:NSLayoutAttributeCenterX
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:Label
                                                       attribute: NSLayoutAttributeCenterX
                                                      multiplier:1
                                                        constant:0]];
// Top constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:Label
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.topLayoutGuide
                                                      attribute: NSLayoutAttributeBottom
                                                     multiplier:1
                                                       constant:20]];