Label自适应高度的用法及设置倒角

    UILabel *label = [[UILabel alloc] init];
    
    //根据内容动态计算label的高度
    label.text = @"Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions";
    
    //1.获取label当前使用的字体
    UIFont *labelFont = label.font;
    
    //2.根据label的内容和字体来计算所需大小
    NSString *labelContent = label.text;
    //将内容放到一个矩形区域内来计算实际需要的大小
    //宽为最后的实际宽度
    //高必须要比内容大,不然无法计算出真正的高度
    CGSize size = CGSizeMake(300, 1000);
    //用指定字体在指定区域内计算出所需的实际大小,此方法虽被苹果划红线,但仍可用
    CGSize realSize = [labelContent sizeWithFont:labelFont constrainedToSize:size];
    
    //3.重新设置label的大小
    label.frame = CGRectMake(10, 30, realSize.width, realSize.height);
    
    //4.设置行数
    label.numberOfLines = 0;
    
    label.backgroundColor = [UIColor redColor];
    
    //设置Label倒角
    label.layer.cornerRadius = 10;
    label.clipsToBounds = YES;
    
    [self.window addSubview:label];
    
    
    //圆形的view
    UIView *v = [[UIView alloc] init];
    v.frame = CGRectMake(10, 300, 100, 100);
    
    v.layer.cornerRadius = 50;
    v.clipsToBounds = YES;
    
    v.backgroundColor = [UIColor redColor];
    [self.window addSubview:v];

 

posted @ 2015-05-11 23:59  热血博  阅读(509)  评论(0编辑  收藏  举报