首页 > 代码库 > UILable自适应frame

UILable自适应frame

UILabel *textlab = [[UILabel alloc]initWithFrame:CGRectMake(20, 10,ScrollView.frame.size.width - 40,260)];

    textlab.text = @"测试用的随便写写ffffffffeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

    textlab.backgroundColor = [UIColor clearColor];

    textlab.textColor = [UIColor blackColor];

    /* 设置字号大小*/

    textlab.font = [UIFont systemFontOfSize:16.0];

    textlab.textAlignment = NSTextAlignmentLeft;

    /* 字符串太长自动换行 */

    textlab.numberOfLines = 0;

    textlab.lineBreakMode =NSLineBreakByWordWrapping;

    

    //添加此项可以设置自适应

    CGSize size = CGSizeMake(280,2000);//宽度高度上限

    CGSize labelsize = [textlab.text sizeWithFont:textlab.font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];

    [textlab setFrame:CGRectMake(20, 20, labelsize.width, labelsize.height)];

    [ScrollView addSubview:textlab];