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

时间:2015-06-08 07:54:38

标签: ios uiscrollview autolayout

大家好,               在这里,我从故事板中获取了一个UIView和UIScrollview,并从storyboard设置了约束,我在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;
}

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

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

lbl_title = [[UILabel alloc]init];

使用

lbl_title = [[UILabel alloc]initWithFrame:];