iOS UILabel自动适应开度和高度

2015-07-11 0 717
iOS UILabel自动适应开度和高度

处理UILabel根据文字长度和大小自动适应宽度和高度

   //这个frame是初设的,没关系,后面还会重新设置其size。
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
     
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],};
     
    NSString *str = @"abcdefg你上课可是你的拿到了";
    CGSize textSize = [str boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
     
    [label setFrame:CGRectMake(100, 100, textSize.width, textSize.height)];
    label.textColor = [UIColor greenColor];
    label.text = str;
    [self.view addSubview:label];

遇见资源网 objective-c iOS UILabel自动适应开度和高度 http://www.ox520.com/17968.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务