首页 > 代码库 > 自定义uilabel的高度

自定义uilabel的高度

 例子 :


UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 200, 30)];

    label.text = @"SAFDFDSFSDFDAFDFLINAJIDN;OHAFIlsjfgdgljdfgSAFDFDSFSDFDAFDFLINAJIDN;OHAFIlsjfgdgljdfgSAFDFDSFSDFDAFDFLINAJIDN;OHAFIlsjfgdgljdfgSAFDFgfhfgjlinjiasdfsddfgdgdfgdfgfdgdgdwerterteteternlgonhjgjgjjg";

    label.backgroundColor = [UIColor yellowColor];

    //拿到label的字体大小

    UIFont *font = label.font;

    //label.numberOfLines 设置为 0时及不固定label的行数

    label.numberOfLines = 0 ;

    

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil];

    CGSize size = [label.text boundingRectWithSize:CGSizeMake(32, 1000) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    

    label.frame = CGRectMake(0, 100, 320, size.height);