UILabel自适应高度

长平狐 发布于 2013/12/25 17:17
阅读 128
收藏 0
UILabel *instructions=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 180)];
    NSString *text = @"这是个测试,不信你试试!这是个测试,再不信你copy代码!这是个测试,果然!自适应高度了!";
    instructions.text = text;
    instructions.lineBreakMode = NSLineBreakByWordWrapping; //这个是关键
    instructions.numberOfLines = 0;  //这个是关键
    [instructions setTextColor:[UIColor grayColor]];
    CGSize size = [instructions.text sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:CGSizeMake(300.0f,CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    
    CGRect newFrame = instructions.frame;
    
    newFrame.size.height = size.height;
    instructions.frame = newFrame;
    [instructions sizeToFit];
    
    [self.view addSubview:instructions];


原文链接:http://blog.csdn.net/rhljiayou/article/details/8705581
加载中
返回顶部
顶部