我怎样才能得到 UILabel 的高度?

how can i get the height of UILabel?

大家好, 在这里,我从故事板中取出了一个 UIView 和 UIScrollview,并从故事板中设置了它的约束,我在 UIView 内部以编程方式创建了一个 UILabel 并设置了它的约束。现在我的问题是如何获得 UILabel 的高度?所以根据它的高度,我想计算 UIView 的高度,还需要设置 scrollview 的内容大小。

这是我的代码

UIViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property(nonatomic, weak)IBOutlet UIScrollView *scrlview;
@property(nonatomic, weak) IBOutlet UIView *my_view;
@property(nonatomic, retain)IBOutlet NSLayoutConstraint *heightconstraint;
@property(nonatomic, strong) UILabel *lbl_title;

@end


UIViewController.m 

@implementation ViewController
@synthesize scrlview,my_view,heightconstraint,lbl_title;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title=@"Demo";
    NSString *text3 = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent non quam ac massa viverra semper. Maecenas mattis justo ac augue volutpat congue. Maecenas laoreet, nulla eu faucibus gravida, felis orci dictum risus, sed sodales sem eros eget risus. Morbi imperdiet sed diam et sodales. Vestibulum ut est id mauris ultrices gravida. Nulla malesuada metus ut erat malesuada, vitae ornare neque semper. Aenean a commodo justo, vel placerat odio. Curabitur vitae consequat tortor. Aenean eu magna ante. Integer tristique elit ac augue laoreet, eget pulvinar lacus dictum. Cras eleifend lacus eget pharetra elementum. Etiam fermentum eu felis eu tristique. Integer eu purus vitae turpis blandit consectetur. Nulla facilisi. Praesent bibendum massa eu metus pulvinar, quis tristique nunc commodo. Ut varius aliquam elit, a tincidunt elit aliquam non. Nunc ac leo purus. Proin condimentum placerat ligula, at tristique neque scelerisque ut. Suspendisse ut";



    lbl_title = [[UILabel alloc]init];

    [lbl_title setText:text3];
    [lbl_title setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
    [lbl_title setBackgroundColor:[UIColor purpleColor]];
    [lbl_title setTranslatesAutoresizingMaskIntoConstraints:FALSE];
    lbl_title.numberOfLines=0;
    [my_view addSubview:lbl_title];

    [my_view addConstraint:[NSLayoutConstraint constraintWithItem:lbl_title

                                                                 attribute:NSLayoutAttributeLeading

                                                                 relatedBy:NSLayoutRelationEqual

                                                                    toItem:my_view

                                                                 attribute:NSLayoutAttributeLeading

                                                                multiplier:1.0

                                                                  constant:10.0]];



    [my_view addConstraint:[NSLayoutConstraint constraintWithItem:lbl_title

                                                                 attribute:NSLayoutAttributeTop

                                                                 relatedBy:NSLayoutRelationEqual

                                                                    toItem:my_view

                                                                 attribute:NSLayoutAttributeTop

                                                                multiplier:1.0

                                                                  constant:10.0]];





    [my_view addConstraint:[NSLayoutConstraint constraintWithItem:lbl_title

                                                                 attribute:NSLayoutAttributeTrailing

                                                                 relatedBy:NSLayoutRelationEqual

                                                                    toItem:my_view

                                                                 attribute:NSLayoutAttributeTrailing

                                                                multiplier:1.0

                                                                  constant:-10.0]];

NSLog(@"height of label in viewdidload %f",lbl_title.frame.size.height);
}

-(void)viewDidLayoutSubviews{
    NSLog(@"height of label %f",lbl_title.frame.size.height);
   [scrlview setContentSize:CGSizeMake(0, 2000)];
   heightconstraint.constant=2000;
}

试试这个

    label_getTextSize = [[UILabel alloc] initWithFrame:CGRectMake(12, 28, 296, 59)];
    label_getTextSize.lineBreakMode = NSLineBreakByWordWrapping;
    label_getTextSize.numberOfLines = 3;
    
    CGSize labelSize = [label_getTextSize textHeigh:someText attributes:[self getTextStyle]];

=================
-(NSDictionary *) getTextStyle
{
    UIFont * font = [UIFont fontWithName:@"font name" size:10];
    float lineSpacing = 0.0f;
    float kerling = 0.0f;
    
    
    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    [paragrahStyle setLineSpacing:lineSpacing];
    
    NSDictionary * textAttributes = @{
                                NSFontAttributeName : font,
                                NSForegroundColorAttributeName : [UIColor whiteColor],
                                NSKernAttributeName : @(kerling),
                                NSParagraphStyleAttributeName : paragrahStyle
                                };
    
    return textAttributes;
}

您需要使用 UILabel 的框架进行初始化 lbl_title。所以不用

lbl_title = [[UILabel alloc]init];

使用

lbl_title = [[UILabel alloc]initWithFrame:];

如果您使用自动布局,则必须使用 layoutIfNeeded。获取所有 SubView

的帧
    [self.view layoutIfNeeded];
NSLog(@"height of label %f",lbl_title.frame.size.height);